Skip to content

Core: Fix staticDirs path issue on Windows #17641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/core-server/src/utils/__tests__/server-statics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ describe('parseStaticDir', () => {
targetDir: './custom-endpoint',
targetEndpoint: '/custom-endpoint',
});

await expect(parseStaticDir('C:\\foo\\bar:\\custom-endpoint')).resolves.toEqual({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await expect(parseStaticDir('C:\\foo\\bar:\\custom-endpoint')).resolves.toEqual({
await expect(parseStaticDir('C:\\foo\\bar:/custom-endpoint')).resolves.toEqual({

shouldn't this be a forward slash since it's a URL?

Copy link
Contributor Author

@justrhysism justrhysism Mar 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the underlying issue is that getDirectoryFromWorkingDir() in useStatics returns this "URL" with backslashes; see arg in the screencap:

image

I'm sure that parseStaticDir is intended to receive the path as a URL, but the reality is that it might not.

As such, I've left the original test with /custom-endpoint and added \\custom-endpoint to ensure this case is covered.


I did briefly consider fixing the underlying issue inside useStatics, however that seemed like a much riskier fix with a much, much broader footprint. To me the safer course of action was to make parseStaticDir more forgiving (not to mention significantly easier for my first contrib).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification! 🙏

staticDir: expect.any(String), // can't test this properly on unix
staticPath: path.resolve('C:\\foo\\bar'),
targetDir: './custom-endpoint',
targetEndpoint: '/custom-endpoint',
});
});

it('pins relative endpoint at root', async () => {
Expand Down
14 changes: 11 additions & 3 deletions lib/core-server/src/utils/server-statics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ export async function useStatics(router: any, options: Options) {
}

export const parseStaticDir = async (arg: string) => {
// Split on ':' only if not followed by '\', for Windows compatibility (e.g. 'C:\some\dir')
const [rawDir, target = '/'] = arg.split(/:(?!\\)/);
// Split on last index of ':', for Windows compatibility (e.g. 'C:\some\dir:\foo')
const lastColonIndex = arg.lastIndexOf(':');
const isWindowsAbsolute = path.win32.isAbsolute(arg);
const isWindowsRawDirOnly = isWindowsAbsolute && lastColonIndex === 1; // e.g. 'C:\some\dir'
const splitIndex = lastColonIndex !== -1 && !isWindowsRawDirOnly ? lastColonIndex : arg.length;

const targetRaw = arg.substring(splitIndex + 1) || '/';
const target = targetRaw.split(path.sep).join(path.posix.sep); // Ensure target has forward-slash path

const rawDir = arg.substring(0, splitIndex);
const staticDir = path.isAbsolute(rawDir) ? rawDir : `./${rawDir}`;
const staticPath = path.resolve(staticDir);
const targetDir = target.replace(/^\/?/, './');
const targetEndpoint = targetDir.substr(1);
const targetEndpoint = targetDir.substring(1);

if (!(await pathExists(staticPath))) {
throw new Error(
Expand Down