question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

generateFiles doesn't write to outputPath when called inside a watcher

See original GitHub issue

Current 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

  1. Create a .env file with any variable and value
  2. Create a .env.d.ts__tmpl__ file in the same scope as the generator index.ts file with the following code:
declare global {
  namespace NodeJS {
    interface ProcessEnv {
      <% for(const prop in env) { -%>
        <%- JSON.stringify(prop) %>: string;
      <% } %>
    }
  }
}
export {};
  1. 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...') });
  };
}
  1. Run the generator with nx workspace-generator nx-env
  2. Check that the generated .env.d.ts file has been created in the __generated__ folder
  3. Add a new variable to .env or change the existing variable name (not value)
  4. 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:closed
  • Created a year ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
AgentEndercommented, Oct 27, 2022

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.

0reactions
skog-newgluecommented, Oct 27, 2022

@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');, any output calls 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?

Screenshot 2022-10-27 at 08 46 35

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found