generateFiles doesn't write to outputPath when called inside a watcher
See original GitHub issueCurrent Behavior
Calling generateFiles from @nrwl/devkit works fine when doing it once but if you also want to have a watcher listen for file changes it doesn’t create or update the file at the output path when the watcher callback runs.
Expected Behavior
I would expect the same behavior regardless of when generateFiles is called. From my investigations tree.write inside generateFiles runs properly, but the file is never written… I tried looking inside that function to see what is happening but I couldn’t see what is actually writing the file in the first place. I’m not sure if this is a regression or not but most likely isn’t.
Steps to Reproduce
- Create a
.envfile with any variable and value - Create a
.env.d.ts__tmpl__file in the same scope as the generatorindex.tsfile with the following code:
declare global {
namespace NodeJS {
interface ProcessEnv {
<% for(const prop in env) { -%>
<%- JSON.stringify(prop) %>: string;
<% } %>
}
}
}
export {};
- Add a generator with the following code:
import * as dotenv from 'dotenv';
dotenv.config();
import { generateFiles, Tree } from '@nrwl/devkit';
import { output } from '@nrwl/workspace/src/utils/output';
import watch from 'node-watch';
import { NxEnvOptions } from './types';
const NAME = 'nx-env';
const templatesPath = `tools/generators/${NAME}`;
function title(key = '') {
return [NAME, key].filter(Boolean).join(': ');
}
function createTypeDefinitionFile(tree: Tree) {
// Load the updated variables into process.env
dotenv.config();
const outputPath = '__generated__';
generateFiles(tree, templatesPath, outputPath, {
tmpl: '',
env: process.env,
});
output.success({ title: title(`Files generated at: ${outputPath}`) });
}
export default async function (tree: Tree, schema: NxEnvOptions) {
output.note({ title: title('Generating...') });
watch('.env', { persistent: true }, () => {
createTypeDefinitionFile(tree);
});
createTypeDefinitionFile(tree);
return async () => {
output.note({ title: title('Listening for changes...') });
};
}
- Run the generator with
nx workspace-generator nx-env - Check that the generated
.env.d.tsfile has been created in the__generated__folder - Add a new variable to
.envor change the existing variable name (not value) - Notice how the file isn’t updated.
Failure Logs
No errors are thrown.
Environment
Node : 16.18.0
OS : darwin x64
npm : 8.19.2
nx : 14.3.2
@nrwl/angular : 14.3.2
@nrwl/cypress : 14.3.2
@nrwl/detox : Not Found
@nrwl/devkit : 14.3.2
@nrwl/eslint-plugin-nx : 14.3.2
@nrwl/express : Not Found
@nrwl/jest : 14.3.2
@nrwl/js : Not Found
@nrwl/linter : 14.3.2
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : Not Found
@nrwl/nx-cloud : 14.1.1
@nrwl/nx-plugin : Not Found
@nrwl/react : Not Found
@nrwl/react-native : Not Found
@nrwl/schematics : Not Found
@nrwl/storybook : 14.3.2
@nrwl/web : Not Found
@nrwl/workspace : 14.3.2
typescript : 4.7.3
---------------------------------------
Community plugins:
@nguniversal/express-engine: 13.0.2
rxjs: 6.6.7
@nguniversal/builders: 14.0.1
@storybook/angular: 6.5.9
Issue Analytics
- State:
- Created a year ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
File Watchers - Output path to refresh - confusion
Hello,. I've spent almost half day tweaking parameters of File Watcher. The main struggle was filling the Output path to refresh parameter.
Read more >File watcher: "An output directory must be specified when ...
As Lena's explain in her answer, all I had to do was to check the "Create output file from stdout". The arguments are...
Read more >File Watchers - Help | IntelliJ IDEA - JetBrains
In the Name field, type the name of the File Watcher. ... value in the Output paths to refresh field does not make...
Read more >Watch and WatchOptions | webpack
Webpack can watch files and recompile whenever they change. This page explains how to enable this and a couple of tweaks you can...
Read more >rollup.js
You can provide an optional Rollup configuration file to simplify command ... assertions in "es" output --no-externalLiveBindings Do not generate code to ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

You can pass options to execsync to inherit stdio, which is what you want. It should be easy to find on Google/node docs from that.
@AgentEnder Gotcha. Got it working now finally but I found a problem, and I’m not sure if it’s fixable. When you run a generator from within an executor with
execSync('nx g @my-org/automation:env-type-def');, anyoutputcalls within the generator aren’t logged despite the generator running as intended (I can see that it creates the file I want). Here I would expect to see 2 more logs from the generator but they aren’t there. Is this because it’s running in a different process? And is there any way to fix it?