firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.02k stars 933 forks source link

Incorrect path resolve when other VS Code extensions run firebase related command #7471

Open jin-qin opened 3 months ago

jin-qin commented 3 months ago

[REQUIRED] Environment info

firebase-tools: 13.13.3

Platform: macOS Sonoma 14.5

[REQUIRED] Test case

Use Orta.vscode-jest extension, configure firebase emulators:exec "jest" as its command, run jest unit test.

[REQUIRED] Steps to reproduce

  1. Install Orta.vscode-jest extension through VS Code extension marketplace.
  2. Create a bash file say test.sh, and put the following content into it:

    #!/bin/bash
    
    PARAMS="$@"
    yarn firebase emulators:exec "jest ${PARAMS}"
  3. In VS Code settings.json, add "jest.jestCommandLine": "./test.sh" or put ./test.sh into Jest: Jest Command Line in Jest extension settings.
  4. Configure a jest unit test
  5. Run jest unit test through CodeLens (i.e. clicking the run test button in the unit test file)

[REQUIRED] Expected behavior

Unit test is passed.

[REQUIRED] Actual behavior

Received error, test aborted:

TestRun "my-app:runTest: orta.vscode-jest:TestProvider:my-app:32 (0)" started
node:fs:448
    return binding.readFileUtf8(path, stringToFlags(options.flag));
                   ^

Error: ENOENT: no such file or directory, open '/Users/user/Projects/my-app/node_modules/firebase-tools/lib/templates/hosting/init.js'
    at readFileSync (node:fs:448:20)
    at readTemplateSync (/Users/user/Projects/my-app/node_modules/firebase-tools/lib/templates.js:17:34)
    at Object.<anonymous> (/Users/user/Projects/my-app/node_modules/firebase-tools/lib/hosting/implicitInit.js:12:56)
    at Module._compile (node:internal/modules/cjs/loader:1358:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
    at Module.load (node:internal/modules/cjs/loader:1208:32)
    at Module._load (node:internal/modules/cjs/loader:1024:12)
    at Module.require (node:internal/modules/cjs/loader:1233:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (/Users/user/Projects/my-app/node_modules/firebase-tools/lib/frameworks/index.js:25:24) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/Users/user/Projects/my-app/node_modules/firebase-tools/lib/templates/hosting/init.js'
}

Node.js v20.14.0

Workaround

In fact, if I remove the following extension check insrc/templates.ts:

if (isVSCodeExtension()) {
    // In the VSCE, the /templates directory is copied into dist, which makes it
    // right next to the compiled files (from various sources including this
    // TS file). See CopyPlugin in `../firebase-vscode/webpack.common.js`.
    return resolve(__dirname, "templates", relPath);
}

the issue will be suppressed and the unit test is passed.

joehan commented 2 months ago

Hey @jin-qin, thanks for reporting this issue. This behavior is a consequence of some recent work we've been doing on a VSCode extension. We should looks into better ways to detect when we are running as part of VSCode. As a immediate workaround, you can manually set the env var that triggers this behavior:

VSCODE_CWD="" yarn firebase emulators:exec "jest ${PARAMS}"
jin-qin commented 2 months ago

It works, thank you @joehan

i13-ctflora commented 1 month ago

Confirming that I found the same issue trying to start firebase emulators via npm script within the Ionic vscode extension, and that adding the VSCODE_CWD='' environment variable to the npm script did alleviate the issue. The npm script ran fine originally from the command line, but gave this error when executing from the Ionic vscode extension:

Error: ENOENT: no such file or directory, open '/Users/username/.nvm/versions/node/v20.16.0/lib/node_modules/firebase-tools/lib/templates/hosting/init.js'
    at readFileSync (node:fs:448:20)

Here's my updated npm script:

  "scripts": {
    "firebase": "VSCODE_CWD='' firebase emulators:start"
  },