Skip to content

Commit 370524f

Browse files
authored
Merge pull request #34445 from storybookjs/change-detection-windows
Core: Normalize file paths in ChangeDetectionService and trace-changed for Windows support
2 parents 2c99f18 + a09cdf6 commit 370524f

File tree

3 files changed

+20
-35
lines changed

3 files changed

+20
-35
lines changed

code/core/src/core-server/change-detection/ChangeDetectionService.test.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from 'node:path';
1+
import { join } from 'pathe';
22

33
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
44

@@ -637,17 +637,20 @@ describe('ChangeDetectionService', () => {
637637
});
638638

639639
it('stores changed files as normalized repo-relative paths', async () => {
640-
const buttonCss = createModuleNode(join(workingDir, 'src', 'Button.module.css'));
641-
const buttonComponent = createModuleNode(join(workingDir, 'src', 'Button.tsx'));
642-
const buttonStory = createModuleNode(join(workingDir, 'src', 'Button.stories.tsx'));
640+
const buttonCssPath = join(workingDir, 'src', 'Button.module.css');
641+
const buttonComponentPath = join(workingDir, 'src', 'Button.tsx');
642+
const buttonStoryPath = join(workingDir, 'src', 'Button.stories.tsx');
643+
const buttonCss = createModuleNode(buttonCssPath);
644+
const buttonComponent = createModuleNode(buttonComponentPath);
645+
const buttonStory = createModuleNode(buttonStoryPath);
643646

644647
buttonCss.importers.add(buttonComponent);
645648
buttonComponent.importers.add(buttonStory);
646649

647650
const moduleGraph: ModuleGraph = new Map([
648-
[join(workingDir, 'src', 'Button.module.css'), new Set([buttonCss])],
649-
[join(workingDir, 'src', 'Button.tsx'), new Set([buttonComponent])],
650-
[join(workingDir, 'src', 'Button.stories.tsx'), new Set([buttonStory])],
651+
[buttonCssPath, new Set([buttonCss])],
652+
[buttonComponentPath, new Set([buttonComponent])],
653+
[buttonStoryPath, new Set([buttonStory])],
651654
]);
652655
const storyIndex = createStoryIndex([
653656
{ storyId: 'button--primary', importPath: './src/Button.stories.tsx', title: 'Button' },

code/core/src/core-server/change-detection/ChangeDetectionService.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { relative, resolve } from 'node:path';
1+
import { join, relative } from 'pathe';
22

33
import { logger } from 'storybook/internal/node-logger';
44
import type {
@@ -41,23 +41,14 @@ function getStoryIdsByAbsolutePath(
4141
workingDir: string
4242
): Map<string, Set<string>> {
4343
const storyIdsByFile = new Map<string, Set<string>>();
44-
const addStoryId = (filePath: string, storyId: string) => {
45-
const storyIds = storyIdsByFile.get(filePath) ?? new Set<string>();
46-
storyIds.add(storyId);
47-
storyIdsByFile.set(filePath, storyIds);
48-
};
49-
5044
Object.values(storyIndex.entries).forEach((entry) => {
51-
if (entry.type !== 'story' || entry.importPath.startsWith('virtual:')) {
52-
return;
45+
if (entry.type === 'story' && !entry.importPath.startsWith('virtual:')) {
46+
const filePath = join(workingDir, entry.importPath);
47+
const storyIds = storyIdsByFile.get(filePath) ?? new Set<string>();
48+
storyIds.add(entry.id);
49+
storyIdsByFile.set(filePath, storyIds);
5350
}
54-
55-
const absolutePath = resolve(workingDir, entry.importPath);
56-
const normalizedAbsolutePath = normalizePath(absolutePath);
57-
addStoryId(absolutePath, entry.id);
58-
addStoryId(normalizedAbsolutePath, entry.id);
5951
});
60-
6152
return storyIdsByFile;
6253
}
6354

@@ -80,11 +71,6 @@ function mergeStatusValues(
8071
return nextValue;
8172
}
8273

83-
function toRepoRelativePath(repoRoot: string, filePath: string): string {
84-
const relativePath = relative(repoRoot, filePath);
85-
return relativePath.startsWith('\\\\?\\') ? relativePath : relativePath.replace(/\\/g, '/');
86-
}
87-
8874
/**
8975
* Coordinates change detection by listening to builder module-graph updates, resolving changed
9076
* files from git, mapping those changes to affected stories, and publishing the resulting story
@@ -255,12 +241,8 @@ export class ChangeDetectionService {
255241
this.options.storyIndexGeneratorPromise,
256242
]);
257243

258-
const changedFiles = new Set(
259-
Array.from(changes.changed).map((filePath) => normalizePath(resolve(repoRoot, filePath)))
260-
);
261-
const newFiles = new Set(
262-
Array.from(changes.new).map((filePath) => normalizePath(resolve(repoRoot, filePath)))
263-
);
244+
const changedFiles = new Set(Array.from(changes.changed).map((path) => join(repoRoot, path)));
245+
const newFiles = new Set(Array.from(changes.new).map((path) => join(repoRoot, path)));
264246
const scannedFiles = new Set([...changedFiles, ...newFiles]);
265247
const normalizedModuleGraph = new Map<string, Set<ModuleNode>>();
266248
moduleGraph.forEach((nodes, filePath) => {
@@ -302,7 +284,7 @@ export class ChangeDetectionService {
302284
storyIds.forEach((storyId) => {
303285
const existingStatus = statuses.get(storyId);
304286
const changedStoryFiles = new Set<string>(existingStatus?.data?.changedFiles ?? []);
305-
changedStoryFiles.add(toRepoRelativePath(repoRoot, changedFile));
287+
changedStoryFiles.add(relative(repoRoot, changedFile));
306288

307289
statuses.set(storyId, {
308290
storyId,

code/core/src/core-server/change-detection/GitDiffProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { watch, type FSWatcher } from 'node:fs';
22
import { readFile, stat } from 'node:fs/promises';
3-
import { dirname, join, resolve as resolvePath } from 'node:path';
3+
import { dirname, join, resolve as resolvePath } from 'pathe';
44

55
// eslint-disable-next-line depend/ban-dependencies
66
import { execa, type ExecaError } from 'execa';

0 commit comments

Comments
 (0)