Skip to content

Commit 361eb08

Browse files
committed
Resolve relative to host.getCurrentDirectory() with createProgram API
1 parent 6b3b6bb commit 361eb08

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,8 @@ namespace ts {
796796
}
797797
trace(state.host, Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName);
798798
}
799-
return tryLoadModuleUsingPaths(extensions, moduleName, baseUrl ?? Debug.checkDefined(pathsBasePath), paths, loader, /*onlyRecordFailures*/ false, state);
799+
const baseDirectory = baseUrl ?? Debug.checkDefined(pathsBasePath || state.host.getCurrentDirectory?.(), "Encountered 'paths' without a 'baseUrl', config file, or host 'getCurrentDirectory'.");
800+
return tryLoadModuleUsingPaths(extensions, moduleName, baseDirectory, paths, loader, /*onlyRecordFailures*/ false, state);
800801
}
801802
}
802803

src/testRunner/unittests/programApi.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,20 @@ namespace ts {
204204
assert.isEmpty(program.getSemanticDiagnostics());
205205
});
206206
});
207+
208+
describe("unittests:: programApi:: CompilerOptions relative paths", () => {
209+
it("resolves relative paths by getCurrentDirectory", () => {
210+
const main = new documents.TextDocument("/main.ts", "import \"module\";");
211+
const mod = new documents.TextDocument("/lib/module.ts", "declare const foo: any;");
212+
213+
const fs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false, { documents: [main, mod], cwd: "/" });
214+
const program = createProgram(["./main.ts"], {
215+
paths: { "*": ["./lib/*"] }
216+
}, new fakes.CompilerHost(fs, { newLine: NewLineKind.LineFeed }));
217+
218+
assert.isEmpty(program.getConfigFileParsingDiagnostics());
219+
assert.isEmpty(program.getGlobalDiagnostics());
220+
assert.isEmpty(program.getSemanticDiagnostics());
221+
});
222+
});
207223
}

0 commit comments

Comments
 (0)