Skip to content

Commit 36121d6

Browse files
committed
Code review feedback
1 parent a41ae32 commit 36121d6

File tree

5 files changed

+36
-39
lines changed

5 files changed

+36
-39
lines changed

server/src/bsc-args/rewatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function getRuntimePath(
2424
export async function getRewatchBscArgs(
2525
send: (msg: p.Message) => void,
2626
bscBinaryLocation: utils.NormalizedPath | null,
27-
projectsFiles: Map<string, projectFiles>,
27+
projectsFiles: Map<utils.NormalizedPath, projectFiles>,
2828
entry: IncrementallyCompiledFileInfo,
2929
): Promise<RewatchCompilerArgs | null> {
3030
const rewatchCacheEntry = entry.buildRewatch;

server/src/incrementalCompilation.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,24 +263,21 @@ function triggerIncrementalCompilationOfFile(
263263
if (debug()) console.log("Did not find open project for " + filePath);
264264
return;
265265
}
266-
// projectRootPath is already the correct normalized key format
267-
const actualProjectRootPath = projectRootPath;
268266

269267
// computeWorkspaceRootPathFromLockfile returns null if lockfile found (local package) or if no parent found
270-
const computedWorkspaceRoot = utils.computeWorkspaceRootPathFromLockfile(
271-
actualProjectRootPath,
272-
);
268+
const computedWorkspaceRoot =
269+
utils.computeWorkspaceRootPathFromLockfile(projectRootPath);
273270
// If null, it means either a lockfile was found (local package) or no parent project root exists
274271
// In both cases, we default to actualProjectRootPath
275272
const workspaceRootPath: NormalizedPath =
276-
computedWorkspaceRoot ?? actualProjectRootPath;
273+
computedWorkspaceRoot ?? projectRootPath;
277274

278275
// Determine if lockfile was found for debug logging
279276
// If computedWorkspaceRoot is null and actualProjectRootPath is not null, check if parent exists
280277
const foundRewatchLockfileInProjectRoot =
281278
computedWorkspaceRoot == null &&
282-
actualProjectRootPath != null &&
283-
utils.findProjectRootOfFile(actualProjectRootPath, true) != null;
279+
projectRootPath != null &&
280+
utils.findProjectRootOfFile(projectRootPath, true) != null;
284281

285282
if (foundRewatchLockfileInProjectRoot && debug()) {
286283
console.log(
@@ -306,14 +303,14 @@ function triggerIncrementalCompilationOfFile(
306303
: moduleName;
307304

308305
const incrementalFolderPath: NormalizedPath = path.join(
309-
actualProjectRootPath,
306+
projectRootPath,
310307
INCREMENTAL_FILE_FOLDER_LOCATION,
311308
) as NormalizedPath;
312309

313310
let originalTypeFileLocation = path.resolve(
314-
actualProjectRootPath,
311+
projectRootPath,
315312
c.compilerDirPartialPath,
316-
path.relative(actualProjectRootPath, filePath),
313+
path.relative(projectRootPath, filePath),
317314
);
318315

319316
const parsed = path.parse(originalTypeFileLocation);
@@ -333,7 +330,7 @@ function triggerIncrementalCompilationOfFile(
333330
},
334331
project: {
335332
workspaceRootPath,
336-
rootPath: actualProjectRootPath,
333+
rootPath: projectRootPath,
337334
callArgs: Promise.resolve([]),
338335
bscBinaryLocation,
339336
incrementalFolderPath,

server/src/lookup.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,24 @@ const getCompiledFolderName = (moduleFormat: ModuleFormat): string => {
1818
}
1919
};
2020

21-
export const replaceFileExtension = <T extends string>(
22-
filePath: T,
21+
export const replaceFileExtension = (filePath: string, ext: string): string => {
22+
let name = path.basename(filePath, path.extname(filePath));
23+
return path.format({ dir: path.dirname(filePath), name, ext });
24+
};
25+
26+
export const replaceFileExtensionWithNormalizedPath = (
27+
filePath: NormalizedPath,
2328
ext: string,
24-
): T => {
29+
): NormalizedPath => {
2530
let name = path.basename(filePath, path.extname(filePath));
2631
const result = path.format({ dir: path.dirname(filePath), name, ext });
27-
// If input was NormalizedPath, result is still normalized
28-
// If input was string, result is string
29-
return result as T;
32+
// path.format() doesn't preserve normalization, so we need to normalize the result
33+
const normalized = normalizePath(result);
34+
if (normalized == null) {
35+
// Should never happen, but handle gracefully
36+
return result as NormalizedPath;
37+
}
38+
return normalized;
3039
};
3140

3241
// Check if filePartialPath exists at directory and return the joined path,

server/src/server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,10 @@ async function createInterface(msg: p.RequestMessage): Promise<p.Message> {
11811181
let result = typeof response.result === "string" ? response.result : "";
11821182

11831183
try {
1184-
let resiPath = lookup.replaceFileExtension(filePath, c.resiExt);
1184+
let resiPath = lookup.replaceFileExtensionWithNormalizedPath(
1185+
filePath,
1186+
c.resiExt,
1187+
);
11851188
fs.writeFileSync(resiPath, result, { encoding: "utf-8" });
11861189
let response: p.ResponseMessage = {
11871190
jsonrpc: c.jsonrpcVersion,

server/src/utils.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,15 @@ let findProjectRootOfFileInDir = (
104104
};
105105

106106
// TODO: races here?
107-
// TODO: this doesn't handle file:/// scheme
108107
export let findProjectRootOfFile = (
109108
source: NormalizedPath,
110109
allowDir?: boolean,
111110
): NormalizedPath | null => {
112-
// source is already normalized
113-
const normalizedSource = source;
114-
115111
// First look in project files (keys are already normalized)
116112
let foundRootFromProjectFiles: NormalizedPath | null = null;
117113
for (const rootPath of projectsFiles.keys()) {
118114
// Both are normalized, so direct comparison works
119-
if (
120-
normalizedSource.startsWith(rootPath) &&
121-
(!allowDir || normalizedSource !== rootPath)
122-
) {
115+
if (source.startsWith(rootPath) && (!allowDir || source !== rootPath)) {
123116
// Prefer the longest path (most nested)
124117
if (
125118
foundRootFromProjectFiles == null ||
@@ -742,13 +735,9 @@ let parseFileAndRange = (fileAndRange: string) => {
742735
// no location! Though LSP insist that we provide at least a dummy location
743736
const normalizedPath = normalizePath(trimmedFileAndRange);
744737
if (normalizedPath == null) {
745-
return {
746-
file: pathToURI(normalizePath("")!),
747-
range: {
748-
start: { line: 0, character: 0 },
749-
end: { line: 0, character: 0 },
750-
},
751-
};
738+
// If we can't normalize the file path, throw an error
739+
// This should never happen in practice, but we need to handle it
740+
throw new Error(`Failed to normalize file path: ${trimmedFileAndRange}`);
752741
}
753742
return {
754743
file: pathToURI(normalizedPath),
@@ -797,10 +786,9 @@ let parseFileAndRange = (fileAndRange: string) => {
797786
}
798787
const normalizedFile = normalizePath(file);
799788
if (normalizedFile == null) {
800-
return {
801-
file: pathToURI(normalizePath("")!),
802-
range,
803-
};
789+
// If we can't normalize the file path, throw an error
790+
// This should never happen in practice, but we need to handle it
791+
throw new Error(`Failed to normalize file path: ${file}`);
804792
}
805793
return {
806794
file: pathToURI(normalizedFile),

0 commit comments

Comments
 (0)