Skip to content

Commit

Permalink
Merge pull request #835 from chromaui/handle-missing-root-lockfile
Browse files Browse the repository at this point in the history
Support untraced flag in dependency tracing fallback scenario (for pnpm)
  • Loading branch information
ghengeveld authored Oct 13, 2023
2 parents c92d977 + 6a9277c commit ff5c393
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
53 changes: 51 additions & 2 deletions node-src/tasks/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,74 @@ describe('traceChangedFiles', () => {
expect(ctx.onlyStoryFiles).toStrictEqual(Object.keys(deps));
});

it('bails on package.json changes if it fails to retrieve lockfile changes', async () => {
it('bails on package.json changes if it fails to retrieve lockfile changes (fallback scenario)', async () => {
findChangedDependencies.mockRejectedValue(new Error('no lockfile'));
findChangedPackageFiles.mockResolvedValue(['./package.json']);

const packageManifestChanges = [{ changedFiles: ['./package.json'], commit: 'abcdef' }];
const ctx = {
env,
log,
http,
options: {},
sourceDir: '/static/',
fileInfo: { statsPath: '/static/preview-stats.json' },
git: { changedFiles: ['./example.js'] },
git: { changedFiles: ['./example.js', './package.json'], packageManifestChanges },
turboSnap: {},
} as any;
await traceChangedFiles(ctx, {} as any);

expect(ctx.turboSnap.bailReason).toEqual({ changedPackageFiles: ['./package.json'] });
expect(findChangedPackageFiles).toHaveBeenCalledWith(packageManifestChanges);
expect(getDependentStoryFiles).not.toHaveBeenCalled();
});

it('continues story file tracing if no dependencies are changed in package.json (fallback scenario)', async () => {
const deps = { 123: ['./example.stories.js'] };
findChangedDependencies.mockRejectedValue(new Error('no lockfile'));
findChangedPackageFiles.mockResolvedValue([]); // no dependency changes
getDependentStoryFiles.mockResolvedValue(deps);

const packageManifestChanges = [{ changedFiles: ['./package.json'], commit: 'abcdef' }];
const ctx = {
env,
log,
http,
options: {},
sourceDir: '/static/',
fileInfo: { statsPath: '/static/preview-stats.json' },
git: { changedFiles: ['./example.js', './package.json'], packageManifestChanges },
turboSnap: {},
} as any;
await traceChangedFiles(ctx, {} as any);

expect(ctx.turboSnap.bailReason).toBeUndefined();
expect(ctx.onlyStoryFiles).toStrictEqual(Object.keys(deps));
expect(findChangedPackageFiles).toHaveBeenCalledWith(packageManifestChanges);
});

it('ignores dependency changes in untraced package.json files (fallback scenario)', async () => {
const deps = { 123: ['./example.stories.js'] };
findChangedDependencies.mockRejectedValue(new Error('no lockfile'));
findChangedPackageFiles.mockResolvedValue([]);
getDependentStoryFiles.mockResolvedValue(deps);

const packageManifestChanges = [{ changedFiles: ['./package.json'], commit: 'abcdef' }];
const ctx = {
env,
log,
http,
options: { untraced: ['package.json'] },
sourceDir: '/static/',
fileInfo: { statsPath: '/static/preview-stats.json' },
git: { changedFiles: ['./example.js', './package.json'], packageManifestChanges },
turboSnap: {},
} as any;
await traceChangedFiles(ctx, {} as any);

expect(ctx.onlyStoryFiles).toStrictEqual(Object.keys(deps));
expect(findChangedPackageFiles).toHaveBeenCalledWith([]);
});
});

describe('uploadStorybook', () => {
Expand Down
13 changes: 11 additions & 2 deletions node-src/tasks/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getDependentStoryFiles } from '../lib/getDependentStoryFiles';
import { createTask, transitionTo } from '../lib/tasks';
import makeZipFile from '../lib/compress';
import uploadFiles from '../lib/uploadFiles';
import { rewriteErrorMessage, throttle } from '../lib/utils';
import { matchesFile, rewriteErrorMessage, throttle } from '../lib/utils';
import { uploadZip, waitForUnpack } from '../lib/uploadZip';
import deviatingOutputDir from '../ui/messages/warnings/deviatingOutputDir';
import missingStatsFile from '../ui/messages/warnings/missingStatsFile';
Expand Down Expand Up @@ -169,7 +169,16 @@ export const traceChangedFiles = async (ctx: Context, task: Task) => {
}
} else {
ctx.log.warn(`Could not retrieve dependency changes from lockfiles; checking package.json`);
const changedPackageFiles = await findChangedPackageFiles(packageManifestChanges);

const { untraced = [] } = ctx.options;
const tracedPackageManifestChanges = packageManifestChanges
?.map(({ changedFiles, commit }) => ({
changedFiles: changedFiles.filter((f) => !untraced.some((glob) => matchesFile(glob, f))),
commit,
}))
.filter(({ changedFiles }) => changedFiles.length > 0);

const changedPackageFiles = await findChangedPackageFiles(tracedPackageManifestChanges);
if (changedPackageFiles.length > 0) {
ctx.turboSnap.bailReason = { changedPackageFiles };
ctx.log.warn(bailFile({ turboSnap: ctx.turboSnap }));
Expand Down

0 comments on commit ff5c393

Please sign in to comment.