Skip to content

Commit 6943586

Browse files
authored
Swap emulator Node runtime discovery to favor local cache (#2740)
1 parent 77a02b0 commit 6943586

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixes Cloud Function inspection when using standalone binary release (#2740)

src/emulator/functionsEmulator.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,14 @@ export class FunctionsEmulator implements EmulatorInstance {
672672
disabled_features: this.args.disabledRuntimeFeatures,
673673
};
674674
}
675+
/**
676+
* Returns a node major version ("10", "8") or null
677+
* @param frb the current Functions Runtime Bundle
678+
*/
679+
getRequestedNodeRuntimeVersion(frb: FunctionsRuntimeBundle): string | undefined {
680+
const pkg = require(path.join(frb.cwd, "package.json"));
681+
return frb.nodeMajorVersion || (pkg.engines && pkg.engines.node);
682+
}
675683
/**
676684
* Returns the path to a "node" executable to use.
677685
* @param cwd the directory to checkout for a package.json file.
@@ -707,24 +715,24 @@ export class FunctionsEmulator implements EmulatorInstance {
707715
// Will happen if we haven't asked about local version yet
708716
}
709717

710-
// If the requested version is the same as the host, let's use that
711-
if (requestedMajorVersion === hostMajorVersion) {
718+
// If the requested version is already locally available, let's use that
719+
if (requestedMajorVersion === localMajorVersion) {
712720
this.logger.logLabeled(
713721
"SUCCESS",
714722
"functions",
715-
`Using node@${requestedMajorVersion} from host.`
723+
`Using node@${requestedMajorVersion} from local cache.`
716724
);
717-
return process.execPath;
725+
return localNodePath;
718726
}
719727

720-
// If the requested version is already locally available, let's use that
721-
if (localMajorVersion === requestedMajorVersion) {
728+
// If the requested version is the same as the host, let's use that
729+
if (requestedMajorVersion === hostMajorVersion) {
722730
this.logger.logLabeled(
723731
"SUCCESS",
724732
"functions",
725-
`Using node@${requestedMajorVersion} from local cache.`
733+
`Using node@${requestedMajorVersion} from host.`
726734
);
727-
return localNodePath;
735+
return process.execPath;
728736
}
729737

730738
// Otherwise we'll begin the conversational flow to install the correct version locally
@@ -750,8 +758,16 @@ export class FunctionsEmulator implements EmulatorInstance {
750758
}
751759

752760
if (this.args.debugPort) {
753-
const { host } = this.getInfo();
754-
args.unshift(`--inspect=${host}:${this.args.debugPort}`);
761+
if (process.env.FIREPIT_VERSION && process.execPath == opts.nodeBinary) {
762+
const requestedMajorNodeVersion = this.getRequestedNodeRuntimeVersion(frb);
763+
this.logger.log(
764+
"WARN",
765+
`To enable function inspection, please run "${process.execPath} is:npm i node@${requestedMajorNodeVersion} --save-dev" in your functions directory`
766+
);
767+
} else {
768+
const { host } = this.getInfo();
769+
args.unshift(`--inspect=${host}:${this.args.debugPort}`);
770+
}
755771
}
756772

757773
const childProcess = spawn(opts.nodeBinary, args, {

0 commit comments

Comments
 (0)