diff --git a/.changeset/pretty-mice-camp.md b/.changeset/pretty-mice-camp.md new file mode 100644 index 0000000000..6e8ed9c039 --- /dev/null +++ b/.changeset/pretty-mice-camp.md @@ -0,0 +1,5 @@ +--- +'@aws-amplify/sandbox': patch +--- + +fix: handling \!pattern in gitignore to avoid excluding everything from watching in sandbox diff --git a/packages/sandbox/src/file_watching_sandbox.test.ts b/packages/sandbox/src/file_watching_sandbox.test.ts index ec02915b2d..8212dd67ac 100644 --- a/packages/sandbox/src/file_watching_sandbox.test.ts +++ b/packages/sandbox/src/file_watching_sandbox.test.ts @@ -713,6 +713,47 @@ void describe('Sandbox using local project name resolver', () => { assert.strictEqual(backendDeployerDeployMock.mock.callCount(), 1); }); + void it('handles !files in .gitignore and filter them out', async (contextual) => { + contextual.mock.method(fs, 'existsSync', () => true); + contextual.mock.method(parseGitIgnore, 'parse', () => { + return { + patterns: [ + '/patternWithLeadingSlash', + 'patternWithoutLeadingSlash', + 'someFile.js', + '!patternThatShouldNotBeIncluded', + 'overlap/', + 'overlap/file', + ], + }; + }); + ({ sandboxInstance, fileChangeEventCallback } = await setupAndStartSandbox({ + executor: sandboxExecutor, + cfnClient: cfnClientMock, + exclude: ['customer_exclude1', 'customer_exclude2'], + })); + await fileChangeEventCallback(null, [ + { type: 'update', path: 'foo/test1.ts' }, + ]); + + // File watcher should be called with right excludes and not include patternThatShouldNotBeIncluded + assert.deepStrictEqual(subscribeMock.mock.calls[0].arguments[2], { + ignore: [ + '.amplify', + 'patternWithLeadingSlash', + 'patternWithoutLeadingSlash', + 'someFile.js', + 'overlap/', + 'overlap/file', + 'customer_exclude1', + 'customer_exclude2', + ], + }); + + // BackendDeployer should also be called once + assert.strictEqual(backendDeployerDeployMock.mock.callCount(), 1); + }); + void it('emits the successfulDeployment event after deployment', async () => { const mockListener = mock.fn(); ({ sandboxInstance, fileChangeEventCallback } = await setupAndStartSandbox({ diff --git a/packages/sandbox/src/file_watching_sandbox.ts b/packages/sandbox/src/file_watching_sandbox.ts index 72586b9144..b98752c3e5 100644 --- a/packages/sandbox/src/file_watching_sandbox.ts +++ b/packages/sandbox/src/file_watching_sandbox.ts @@ -251,7 +251,18 @@ export class FileWatchingSandbox extends EventEmitter implements Sandbox { .parse(gitIgnoreFilePath) .patterns.map((pattern: string) => pattern.startsWith('/') ? pattern.substring(1) : pattern - ); + ) + .filter((pattern: string) => { + if (pattern.startsWith('!')) { + console.log( + `[Sandbox] Pattern ${pattern} found in .gitignore. "${pattern.substring( + 1 + )}" will not be watched if other patterns in .gitignore are excluding it.` + ); + return false; + } + return true; + }); } return []; };