fix: resolve absolute-path imports in monaco ts editor (#9213)

* fix: resolve absolute-path imports in monaco ts editor

* fix: dispose absolute-path extra libs on editor teardown and reset

* fix: skip late ata local-file callbacks after editor teardown
This commit is contained in:
hugocasa
2026-05-18 16:45:19 +00:00
committed by GitHub
parent fec4008696
commit 156eb0b045
2 changed files with 14 additions and 4 deletions
+1 -1
View File
@@ -136,7 +136,7 @@ export const setupTypeAcquisition = (config: ATABootstrapConfig) => {
: '/' + config.scriptPath + (f.raw.startsWith('../') ? '/../' : '/.') + f.raw
let url = config.root + path
let localPath = f.raw
if (f.raw.startsWith('.') && !f.raw.endsWith('.ts')) {
if ((f.raw.startsWith('.') || f.raw.startsWith('/')) && !f.raw.endsWith('.ts')) {
url += '.ts'
localPath += '.ts'
}
+13 -3
View File
@@ -204,6 +204,7 @@
let lastWsAttempt: Date = new Date()
let nbWsAttempt = 0
let disposeMethod: (() => void) | undefined
const absolutePathExtraLibs = new Map<string, { dispose: () => void }>()
const dispatch = createEventDispatcher()
// let graphqlService: MonacoGraphQLAPI | undefined = undefined
@@ -1644,6 +1645,8 @@
(scriptLang == 'bun' || scriptLang == 'tsx' || scriptLang == 'bunnative') &&
ata == undefined
) {
absolutePathExtraLibs.forEach((d) => d.dispose())
absolutePathExtraLibs.clear()
const hostname = getHostname()
const addLibraryToRuntime = async (code: string, _path: string) => {
@@ -1659,12 +1662,17 @@
}
const addLocalFile = async (code: string, _path: string) => {
if (destroyed) return
let p = new URL(_path, uri).href
// if (_path?.startsWith('/')) {
// p = 'file://' + p
// }
let nuri = mUri.parse(p)
console.log('adding local file', _path, nuri.toString())
// Monaco's TS service resolves relative imports against the importer's URI (finding the
// model), but absolute paths like "/u/admin/foo" are looked up as raw paths and miss the
// `file://` model. Register them as extra libs so TS can resolve them.
if (_path.startsWith('/')) {
absolutePathExtraLibs.get(_path)?.dispose()
absolutePathExtraLibs.set(_path, typescriptDefaults.addExtraLib(code, _path))
}
if (editor) {
let localModel = meditor.getModel(nuri)
if (localModel) {
@@ -1783,6 +1791,8 @@
timeoutModel && clearTimeout(timeoutModel)
loadTimeout && clearTimeout(loadTimeout)
aiChatEditorHandler?.clear()
absolutePathExtraLibs.forEach((d) => d.dispose())
absolutePathExtraLibs.clear()
})
async function genRoot(hostname: string) {