Skip to content

Commit 85f8e93

Browse files
committed
fix: preserve sitecustomize import with python gunicorn shim
1 parent 121d4c6 commit 85f8e93

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

src/emulator/functionsEmulator.spec.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ describe("FunctionsEmulator", () => {
6969
expect(shim).to.include("PathFinder.find_spec(");
7070
expect(shim).to.include('"sitecustomize", _firebase_remaining_paths');
7171
expect(shim).to.include("module_from_spec");
72+
expect(shim).to.include('sys.modules["sitecustomize"] = _firebase_user_sitecustomize_module');
7273
expect(shim).to.include("exec_module");
7374
expect(logLabeled).to.have.been.calledOnceWith(
7475
"BULLET",
@@ -156,10 +157,12 @@ describe("FunctionsEmulator", () => {
156157
expect(log).to.not.have.been.called;
157158
});
158159

159-
it("should log a debug message and continue shutdown when shim dir cleanup fails", async () => {
160+
it("should recreate the shim dir after cleanup fails with a non-ENOENT error", async () => {
160161
sandbox.stub(portfinder, "getPortPromise").resolves(9595);
161-
sandbox.stub(functionsPython, "runWithVirtualEnv").returns({} as any);
162-
sandbox.stub(fs, "mkdtempSync").returns("/tmp/firebase-tools-python-shim-error");
162+
const runWithVirtualEnv = sandbox.stub(functionsPython, "runWithVirtualEnv").returns({} as any);
163+
const mkdtempSync = sandbox.stub(fs, "mkdtempSync");
164+
mkdtempSync.onFirstCall().returns("/tmp/firebase-tools-python-shim-error");
165+
mkdtempSync.onSecondCall().returns("/tmp/firebase-tools-python-shim-recreated");
163166
sandbox.stub(fs, "writeFileSync");
164167
const rmSync = sandbox.stub(fs, "rmSync").throws(new Error("permission denied"));
165168

@@ -168,10 +171,15 @@ describe("FunctionsEmulator", () => {
168171

169172
await (emulator as any).startPython(makePythonBackend(), {});
170173
await emulator.stop();
174+
await (emulator as any).startPython(makePythonBackend(), {});
171175

172176
expect(rmSync).to.have.been.calledOnceWith("/tmp/firebase-tools-python-shim-error", {
173177
recursive: true,
174178
});
179+
expect(mkdtempSync).to.have.been.calledTwice;
180+
expect(runWithVirtualEnv.secondCall.args[2].PYTHONPATH).to.equal(
181+
"/tmp/firebase-tools-python-shim-recreated",
182+
);
175183
expect(log).to.have.been.calledWith(
176184
"DEBUG",
177185
sinon.match("Failed to clean up python-disable-gunicorn shim dir"),

src/emulator/functionsEmulator.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ if (
113113
_firebase_user_sitecustomize_module = importlib.util.module_from_spec(
114114
_firebase_user_sitecustomize
115115
)
116+
sys.modules["sitecustomize"] = _firebase_user_sitecustomize_module
116117
_firebase_user_sitecustomize.loader.exec_module(_firebase_user_sitecustomize_module)
117118
`;
118119

@@ -138,18 +139,17 @@ function cleanupPythonDisableGunicornShimDir(logDebug: (message: string) => void
138139
return;
139140
}
140141

142+
const shimDir = pythonDisableGunicornShimDir;
143+
pythonDisableGunicornShimDir = undefined;
144+
141145
try {
142-
fs.rmSync(pythonDisableGunicornShimDir, { recursive: true });
143-
pythonDisableGunicornShimDir = undefined;
146+
fs.rmSync(shimDir, { recursive: true });
144147
} catch (e: any) {
145148
if (e?.code === "ENOENT") {
146-
pythonDisableGunicornShimDir = undefined;
147149
return;
148150
}
149151

150-
logDebug(
151-
`Failed to clean up python-disable-gunicorn shim dir ${pythonDisableGunicornShimDir}: ${e}`,
152-
);
152+
logDebug(`Failed to clean up python-disable-gunicorn shim dir ${shimDir}: ${e}`);
153153
}
154154
}
155155

0 commit comments

Comments
 (0)