Incorrect path resolve when other VS Code extensions run firebase related command
[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
- Install Orta.vscode-jest extension through VS Code extension marketplace.
- Create a bash file say
test.sh, and put the following content into it:#!/bin/bash PARAMS="$@" yarn firebase emulators:exec "jest ${PARAMS}" - In VS Code
settings.json, add"jest.jestCommandLine": "./test.sh"or put./test.shintoJest: Jest Command Linein Jest extension settings. - Configure a jest unit test
- 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.
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}"
It works, thank you @joehan
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"
},
Confirmed having problem in VSCode extension. Thanks for the workaround!
Here's how I bypassed it:
var env = process.env
env.VSCODE_CWD = ''
spawn('/path/to/firebase', ['projects:list', '--json'], { env: env })
The key point is not to forget to include the original environment variables.
Ran into the same issue ourselves when invoking the Firebase emulators from inside a Playwrite global setup script after running the Playwrite test from the ms-playwright VSCode extension. Setting process.env.VSCODE_CWD = '', like for those above, resolved our issue.