Skip to content

Commit 45d042f

Browse files
Fix the open compiled command for monorepos where the package's gets superseded
1 parent a146d5d commit 45d042f

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

server/src/utils.ts

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export const toCamelCase = (text: string): string => {
219219
.replace(/(\s|-)+/g, "");
220220
};
221221

222-
const readBsConfig = (projDir: p.DocumentUri) => {
222+
const readBsConfig = (projDir: p.DocumentUri): any | null => {
223223
try {
224224
let bsconfigFile = fs.readFileSync(
225225
path.join(projDir, c.bsconfigPartialPath),
@@ -270,19 +270,7 @@ let getCompiledFolderName = (moduleFormat: string): string => {
270270
}
271271
};
272272

273-
export let getCompiledFilePath = (
274-
filePath: string,
275-
projDir: string
276-
): execResult => {
277-
let bsconfig = readBsConfig(projDir);
278-
279-
if (!bsconfig) {
280-
return {
281-
kind: "error",
282-
error: "Could not read bsconfig",
283-
};
284-
}
285-
273+
let getSuffixAndPathFragmentFromBsconfig = (bsconfig: any) => {
286274
let pkgSpecs = bsconfig["package-specs"];
287275
let pathFragment = "";
288276
let moduleFormatObj: any = {};
@@ -318,10 +306,53 @@ export let getCompiledFilePath = (
318306
suffix = bsconfig.suffix;
319307
}
320308

309+
return [suffix, pathFragment];
310+
};
311+
312+
export let getCompiledFilePath = (
313+
filePath: string,
314+
projDir: string
315+
): execResult => {
316+
let bsconfig = readBsConfig(projDir);
317+
let error: execResult = {
318+
kind: "error",
319+
error: "Could not read bsconfig",
320+
};
321+
322+
if (!bsconfig) {
323+
return error;
324+
}
325+
326+
let [suffix, pathFragment] = getSuffixAndPathFragmentFromBsconfig(bsconfig);
321327
let partialFilePath = filePath.split(projDir)[1];
322328
let compiledPartialPath = replaceFileExtension(partialFilePath, suffix);
323329
let result = path.join(projDir, pathFragment, compiledPartialPath);
324330

331+
// If the file is not found, lookup a possible root bsconfig that may contain
332+
// info about the possible location of the file.
333+
if (!fs.existsSync(result)) {
334+
let rootBsConfigPath = findFilePathFromProjectRoot(
335+
path.join("..", projDir),
336+
c.bsconfigPartialPath
337+
);
338+
339+
if (!rootBsConfigPath) {
340+
return error;
341+
}
342+
343+
let rootBsconfig = readBsConfig(path.dirname(rootBsConfigPath));
344+
345+
if (!rootBsconfig) {
346+
return error;
347+
}
348+
349+
let [rootSuffix, rootPathFragment] =
350+
getSuffixAndPathFragmentFromBsconfig(rootBsconfig);
351+
352+
let compiledPartialPath = replaceFileExtension(partialFilePath, rootSuffix);
353+
result = path.join(projDir, rootPathFragment, compiledPartialPath);
354+
}
355+
325356
return {
326357
kind: "success",
327358
result,

0 commit comments

Comments
 (0)