Skip to content

Commit b738403

Browse files
authored
fix: find nearest deno config with compiler options (#3096)
Previously it would only consult the compiler options in the workspace root deno.json, now it also considers relevant workspace members.
1 parent c11cf7b commit b738403

File tree

1 file changed

+10
-40
lines changed

1 file changed

+10
-40
lines changed

src/dev/builder.ts

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ export class Builder implements FreshBuilder {
157157
}
158158
}
159159

160-
const denoJson = await readDenoConfigForCompilerOptions(app.config.root);
160+
const denoJson = await findNearestDenoConfigWithCompilerOptions(
161+
app.config.root,
162+
);
161163

162164
const jsxImportSource = denoJson.config.compilerOptions?.jsxImportSource;
163165
if (jsxImportSource === undefined) {
@@ -231,12 +233,11 @@ export interface DenoConfig {
231233
};
232234
}
233235

234-
export async function readDenoConfigForCompilerOptions(
236+
export async function findNearestDenoConfigWithCompilerOptions(
235237
directory: string,
236238
): Promise<{ config: DenoConfig; filePath: string }> {
237239
let dir = directory;
238-
const configs: { config: DenoConfig; filePath: string }[] = [];
239-
outer: while (true) {
240+
while (true) {
240241
for (const name of ["deno.json", "deno.jsonc"]) {
241242
const filePath = path.join(dir, name);
242243
try {
@@ -247,10 +248,8 @@ export async function readDenoConfigForCompilerOptions(
247248
} else {
248249
config = JSON.parse(file);
249250
}
250-
configs.push({ config, filePath });
251-
if (config.workspace) {
252-
break outer;
253-
}
251+
if (config.compilerOptions) return { config, filePath };
252+
if (config.workspace) break;
254253
break;
255254
} catch (err) {
256255
if (!(err instanceof Deno.errors.NotFound)) {
@@ -263,36 +262,7 @@ export async function readDenoConfigForCompilerOptions(
263262
dir = parent;
264263
}
265264

266-
if (configs.length === 0) {
267-
throw new Error(
268-
`Could not find a deno.json or deno.jsonc file in the current directory or any parent directory.`,
269-
);
270-
}
271-
272-
const firstConfig = configs[0];
273-
const lastConfig = configs.at(-1);
274-
if (lastConfig?.config.workspace) {
275-
if (lastConfig === firstConfig) return lastConfig;
276-
if (!Array.isArray(lastConfig.config.workspace)) {
277-
throw new Error(
278-
`Expected "workspace" option to be an array in: ${lastConfig.filePath}`,
279-
);
280-
}
281-
const members = lastConfig.config.workspace.map((member) => {
282-
if (typeof member !== "string") {
283-
throw new Error(
284-
`Expected "workspace" member to be a string in: ${lastConfig.filePath}`,
285-
);
286-
}
287-
return path.join(lastConfig.filePath, "..", member);
288-
});
289-
const parent = path.dirname(firstConfig.filePath);
290-
if (!members.includes(parent)) {
291-
return firstConfig;
292-
} else {
293-
return lastConfig;
294-
}
295-
}
296-
297-
return firstConfig;
265+
throw new Error(
266+
`Could not find a deno.json or deno.jsonc file in the current directory or any parent directory that contains a 'compilerOptions' field.`,
267+
);
298268
}

0 commit comments

Comments
 (0)