Skip to content

Commit ebf5981

Browse files
committed
Warn on relative and non-normalized paths
1 parent 9e0776d commit ebf5981

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

packages/react-fs/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"index.browser.js",
1818
"cjs/"
1919
],
20+
"peerDependencies": {
21+
"react": "^17.0.0"
22+
},
2023
"browser": {
2124
"./index.js": "./index.browser.js"
2225
}

packages/react-fs/src/ReactFilesystem.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {Wakeable, Thenable} from 'shared/ReactTypes';
1111

1212
import {unstable_getCacheForType} from 'react';
1313
import * as fs from 'fs/promises';
14-
import {resolve} from 'path';
14+
import {isAbsolute, normalize} from 'path';
1515

1616
const Pending = 0;
1717
const Resolved = 1;
@@ -70,6 +70,27 @@ function readResult<T>(result: Result<T>): T {
7070
}
7171
}
7272

73+
// We don't want to normalize every path ourselves in production.
74+
// However, relative or non-normalized paths will lead to cache misses.
75+
// So we encourage the developer to fix it in DEV and normalize on their end.
76+
function checkPathInDev(path: string) {
77+
if (__DEV__) {
78+
if (!isAbsolute(path)) {
79+
console.error(
80+
'The provided path was not absolute: "%s". ' +
81+
'Convert it to an absolute path first.',
82+
path,
83+
);
84+
} else if (path !== normalize(path)) {
85+
console.error(
86+
'The provided path was not normalized: "%s". ' +
87+
'Convert it to a normalized path first.',
88+
path,
89+
);
90+
}
91+
}
92+
}
93+
7394
function createReadFileCache(): Map<string, Result<Buffer>> {
7495
return new Map();
7596
}
@@ -86,12 +107,12 @@ export function readFile(
86107
},
87108
): string | Buffer {
88109
const map = unstable_getCacheForType(createReadFileCache);
89-
const resolvedPath = resolve(path);
90-
let entry = map.get(resolvedPath);
110+
checkPathInDev(path);
111+
let entry = map.get(path);
91112
if (!entry) {
92-
const thenable = fs.readFile(resolvedPath);
113+
const thenable = fs.readFile(path);
93114
entry = toResult(thenable);
94-
map.set(resolvedPath, entry);
115+
map.set(path, entry);
95116
}
96117
const result: Buffer = readResult(entry);
97118
if (!options) {

0 commit comments

Comments
 (0)