Skip to content

Commit 48d9914

Browse files
authored
Add logging to detect if function app dir doesn't change (#758)
1 parent a5ed77c commit 48d9914

7 files changed

+82
-11
lines changed

src/AppContext.ts

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export interface LegacyRegisteredFunction extends RegisteredFunction {
1515
}
1616

1717
export class AppContext {
18+
functionAppDirectory: string | null | undefined;
19+
constructor(functionAppDirectory: string | null | undefined) {
20+
this.functionAppDirectory = functionAppDirectory;
21+
}
1822
packageJson: PackageJson = {};
1923
/**
2024
* this hook data will be passed to (and set by) all hooks in all scopes

src/WorkerContext.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { IEventStream } from './GrpcClient';
1010
import { InvocationLogContext, LogHookContext } from './hooks/LogHookContext';
1111

1212
class WorkerContext {
13-
app = new AppContext();
13+
app = new AppContext(undefined);
1414
defaultProgrammingModel?: ProgrammingModel;
1515

1616
/**
@@ -54,8 +54,8 @@ class WorkerContext {
5454
}
5555
}
5656

57-
resetApp(): void {
58-
this.app = new AppContext();
57+
resetApp(functionAppDirectory: string | null | undefined): void {
58+
this.app = new AppContext(functionAppDirectory);
5959
this.app.programmingModel = this.defaultProgrammingModel;
6060
}
6161

src/eventHandlers/FunctionEnvironmentReloadHandler.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getWorkerMetadata } from './getWorkerMetadata';
1010
import LogCategory = rpc.RpcLog.RpcLogCategory;
1111
import LogLevel = rpc.RpcLog.Level;
1212
import CapabilitiesUpdateStrategy = rpc.FunctionEnvironmentReloadResponse.CapabilitiesUpdateStrategy;
13+
import * as path from 'path';
1314

1415
/**
1516
* Environment variables from the current process
@@ -27,7 +28,27 @@ export class FunctionEnvironmentReloadHandler extends EventHandler<
2728
}
2829

2930
async handleEvent(msg: rpc.IFunctionEnvironmentReloadRequest): Promise<rpc.IFunctionEnvironmentReloadResponse> {
30-
worker.resetApp();
31+
if (!msg.functionAppDirectory) {
32+
worker.log({
33+
message: `FunctionEnvironmentReload functionAppDirectory is not defined`,
34+
level: LogLevel.Debug,
35+
logCategory: LogCategory.System,
36+
});
37+
}
38+
39+
if (
40+
worker.app.functionAppDirectory &&
41+
msg.functionAppDirectory &&
42+
isPathEqual(worker.app.functionAppDirectory, msg.functionAppDirectory)
43+
) {
44+
worker.log({
45+
message: `FunctionEnvironmentReload functionAppDirectory has not changed`,
46+
level: LogLevel.Debug,
47+
logCategory: LogCategory.System,
48+
});
49+
}
50+
51+
worker.resetApp(msg.functionAppDirectory);
3152

3253
const response = this.getDefaultResponse(msg);
3354

@@ -63,3 +84,7 @@ export class FunctionEnvironmentReloadHandler extends EventHandler<
6384
return response;
6485
}
6586
}
87+
88+
function isPathEqual(path1: string, path2: string): boolean {
89+
return path.relative(path1, path2) === '';
90+
}

src/eventHandlers/WorkerInitHandler.ts

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ export class WorkerInitHandler extends EventHandler<'workerInitRequest', 'worker
2727
}
2828

2929
async handleEvent(msg: rpc.IWorkerInitRequest): Promise<rpc.IWorkerInitResponse> {
30+
if (!msg.functionAppDirectory) {
31+
worker.log({
32+
message: `WorkerInit functionAppDirectory is not defined`,
33+
level: LogLevel.Debug,
34+
logCategory: LogCategory.System,
35+
});
36+
}
37+
worker.app.functionAppDirectory = msg.functionAppDirectory;
38+
3039
const response = this.getDefaultResponse(msg);
3140

3241
worker.log({

test/eventHandlers/FunctionEnvironmentReloadHandler.test.ts

+31-6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
3838
functionAppDirectory: null,
3939
},
4040
});
41-
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(2), msg.envReload.response);
41+
await stream.assertCalledWith(
42+
msg.envReload.funcAppDirNotDefined,
43+
msg.envReload.reloadEnvVarsLog(2),
44+
msg.envReload.response
45+
);
4246
expect(process.env.hello).to.equal('world');
4347
expect(process.env.SystemDrive).to.equal('Q:');
4448
expect(process.env.PlaceholderVariable).to.be.undefined;
@@ -56,7 +60,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
5660
functionAppDirectory: null,
5761
},
5862
});
59-
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(2), msg.envReload.response);
63+
await stream.assertCalledWith(
64+
msg.envReload.funcAppDirNotDefined,
65+
msg.envReload.reloadEnvVarsLog(2),
66+
msg.envReload.response
67+
);
6068
expect(process.env.hello).to.equal('world');
6169
expect(process.env.SystemDrive).to.equal('Q:');
6270
expect(process.env.PlaceholderVariable).to.be.undefined;
@@ -80,7 +88,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
8088
functionAppDirectory: null,
8189
},
8290
});
83-
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(0), msg.envReload.response);
91+
await stream.assertCalledWith(
92+
msg.envReload.funcAppDirNotDefined,
93+
msg.envReload.reloadEnvVarsLog(0),
94+
msg.envReload.response
95+
);
8496
expect(process.env).to.be.empty;
8597
});
8698

@@ -92,7 +104,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
92104
functionAppDirectory: null,
93105
},
94106
});
95-
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(0), msg.envReload.response);
107+
await stream.assertCalledWith(
108+
msg.envReload.funcAppDirNotDefined,
109+
msg.envReload.reloadEnvVarsLog(0),
110+
msg.envReload.response
111+
);
96112

97113
stream.addTestMessage({
98114
requestId: 'testReqId',
@@ -108,7 +124,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
108124
functionAppDirectory: null,
109125
},
110126
});
111-
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(0), msg.envReload.response);
127+
await stream.assertCalledWith(
128+
msg.envReload.funcAppDirNotDefined,
129+
msg.envReload.reloadEnvVarsLog(0),
130+
msg.envReload.response
131+
);
112132
});
113133

114134
it('reloads environment variable and keeps cwd without functionAppDirectory', async () => {
@@ -123,7 +143,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
123143
functionAppDirectory: null,
124144
},
125145
});
126-
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(2), msg.envReload.response);
146+
await stream.assertCalledWith(
147+
msg.envReload.funcAppDirNotDefined,
148+
msg.envReload.reloadEnvVarsLog(2),
149+
msg.envReload.response
150+
);
127151
expect(process.env.hello).to.equal('world');
128152
expect(process.env.SystemDrive).to.equal('Q:');
129153
expect(process.cwd() == cwd);
@@ -181,6 +205,7 @@ describe('FunctionEnvironmentReloadHandler', () => {
181205
},
182206
});
183207
await stream.assertCalledWith(
208+
msg.envReload.funcAppDirNotChanged,
184209
msg.envReload.reloadEnvVarsLog(0),
185210
msg.envReload.changingCwdLog(testAppPath),
186211
msg.envReload.response

test/eventHandlers/TestEventStream.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class TestEventStream extends EventEmitter implements IEventStream {
102102
await fs.writeFile(testPackageJsonPath, '{}');
103103

104104
worker._hostVersion = undefined;
105-
worker.resetApp();
105+
worker.resetApp(this.originalCwd);
106106

107107
// minor delay so that it's more likely extraneous messages are associated with this test as opposed to leaking into the next test
108108
await new Promise((resolve) => setTimeout(resolve, 20));

test/eventHandlers/msg.ts

+8
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ export namespace msg {
193193
return msg.infoLog(`Changing current working directory to ${dir}`);
194194
}
195195

196+
export const funcAppDirNotDefined = msg.debugLog(
197+
'FunctionEnvironmentReload functionAppDirectory is not defined'
198+
);
199+
200+
export const funcAppDirNotChanged = msg.debugLog(
201+
'FunctionEnvironmentReload functionAppDirectory has not changed'
202+
);
203+
196204
export const response = new RegExpStreamingMessage(
197205
{
198206
requestId: 'testReqId',

0 commit comments

Comments
 (0)