diff --git a/ets2panda/driver/build_system/src/build/generate_arktsconfig.ts b/ets2panda/driver/build_system/src/build/generate_arktsconfig.ts index 87737ee110218123644ebaa8b6107353ca0272a2..c9b8145109ab6c7d7a1b05050a5407e2f0494e41 100644 --- a/ets2panda/driver/build_system/src/build/generate_arktsconfig.ts +++ b/ets2panda/driver/build_system/src/build/generate_arktsconfig.ts @@ -55,13 +55,15 @@ import { export class ArkTSConfig { config: ArkTSConfigObject; - constructor(moduleInfo: ModuleInfo) { + constructor(moduleInfo: ModuleInfo, cacheDir: string) { this.config = { compilerOptions: { package: moduleInfo.packageName, baseUrl: path.resolve(moduleInfo.moduleRootPath, moduleInfo.sourceRoots[0]), paths: {}, - dependencies: {} + dependencies: {}, + rootDir: path.resolve(moduleInfo.moduleRootPath), + cacheDir: path.resolve(cacheDir, moduleInfo.packageName), } }; } @@ -315,7 +317,7 @@ export class ArkTSConfigGenerator { ); this.logger.printErrorAndExit(logData); } - let arktsConfig: ArkTSConfig = new ArkTSConfig(moduleInfo); + let arktsConfig: ArkTSConfig = new ArkTSConfig(moduleInfo, this.buildConfig.cachePath); this.arktsconfigs.set(moduleInfo.packageName, arktsConfig); this.getPathSection(moduleInfo, arktsConfig); diff --git a/ets2panda/driver/build_system/src/types.ts b/ets2panda/driver/build_system/src/types.ts index e7f74ebbb09296a86ba2e686030482d293b455fa..19f8c42da3e8d48dfb4db16188a13d3f8336b914 100644 --- a/ets2panda/driver/build_system/src/types.ts +++ b/ets2panda/driver/build_system/src/types.ts @@ -332,6 +332,8 @@ export interface ArkTSConfigObject { paths: Record; dependencies: Record; useEmptyPackage?: boolean; + rootDir?: string, + cacheDir?: string, } }; diff --git a/ets2panda/util/importPathManager.cpp b/ets2panda/util/importPathManager.cpp index 1060550d2cc88b2e6d2639cf419174f3a6124db0..a00da5ba76debeb7ff6147d5762c3f6a9fe042b3 100644 --- a/ets2panda/util/importPathManager.cpp +++ b/ets2panda/util/importPathManager.cpp @@ -498,8 +498,29 @@ util::StringView ImportPathManager::FormModuleNameSolelyByAbsolutePath(const uti return util::UString(name, allocator_).View(); } -template -static std::string TryFormDynamicModuleName(const DynamicPaths &dynPaths, const ModuleNameFormer &tryFormModuleName) +// should be implemented with a stable name -> path mapping list +static std::optional TryFormModuleName(std::string filePath, std::string_view unitName, + std::string_view unitPath, std::string_view cachePath) +{ + if (cachePath.empty() && filePath.rfind(unitPath, 0) != 0) { + return std::nullopt; + } + if (!cachePath.empty() && filePath.rfind(cachePath, 0) != 0 && filePath.rfind(unitPath, 0) != 0) { + return std::nullopt; + } + std::string_view actualUnitPath = unitPath; + if (!cachePath.empty() && filePath.rfind(cachePath, 0) == 0) { + actualUnitPath = cachePath; + } + auto relativePath = FormRelativeModuleName(filePath.substr(actualUnitPath.size())); + if (relativePath.empty() || FormUnitName(unitName).empty()) { + return FormUnitName(unitName) + relativePath; + } + return FormUnitName(unitName) + "." + relativePath; +} + +template +static std::string TryFormDynamicModuleName(const DynamicPaths &dynPaths, std::string const filePath) { for (auto const &[unitName, did] : dynPaths) { if (did.Path().empty()) { @@ -507,7 +528,7 @@ static std::string TryFormDynamicModuleName(const DynamicPaths &dynPaths, const // source, and, as soon it won't be parsed, no module should be created. continue; } - if (auto res = tryFormModuleName(unitName, did.Path()); res) { + if (auto res = TryFormModuleName(filePath, unitName, did.Path(), ""); res) { return res.value(); } } @@ -535,38 +556,35 @@ util::StringView ImportPathManager::FormModuleName(const util::Path &path) } std::string const filePath(path.GetAbsolutePath()); - - // should be implemented with a stable name -> path mapping list - auto const tryFormModuleName = [filePath](std::string_view unitName, - std::string_view unitPath) -> std::optional { - if (filePath.rfind(unitPath, 0) != 0) { - return std::nullopt; - } - auto relativePath = FormRelativeModuleName(filePath.substr(unitPath.size())); - return FormUnitName(unitName) + - (relativePath.empty() || FormUnitName(unitName).empty() ? relativePath : ("." + relativePath)); - }; - if (auto res = tryFormModuleName(arktsConfig_->Package(), arktsConfig_->BaseUrl() + pathDelimiter_.data()); res) { + if (auto res = TryFormModuleName(filePath, arktsConfig_->Package(), arktsConfig_->BaseUrl() + pathDelimiter_.data(), + arktsConfig_->CacheDir()); + res) { return util::UString(res.value(), allocator_).View(); } if (!stdLib_.empty()) { - if (auto res = tryFormModuleName("std", stdLib_ + pathDelimiter_.at(0) + "std"); res) { + if (auto res = + TryFormModuleName(filePath, "std", stdLib_ + pathDelimiter_.at(0) + "std", arktsConfig_->CacheDir()); + res) { return util::UString(res.value(), allocator_).View(); } - if (auto res = tryFormModuleName("escompat", stdLib_ + pathDelimiter_.at(0) + "escompat"); res) { + if (auto res = TryFormModuleName(filePath, "escompat", stdLib_ + pathDelimiter_.at(0) + "escompat", + arktsConfig_->CacheDir()); + res) { return util::UString(res.value(), allocator_).View(); } } for (auto const &[unitName, unitPath] : arktsConfig_->Paths()) { - if (auto res = tryFormModuleName(unitName, unitPath[0]); res) { + if (auto res = TryFormModuleName(filePath, unitName, unitPath[0], arktsConfig_->CacheDir()); res) { return util::UString(res.value(), allocator_).View(); } } - if (auto dmn = TryFormDynamicModuleName(arktsConfig_->Dependencies(), tryFormModuleName); !dmn.empty()) { + if (auto dmn = TryFormDynamicModuleName(arktsConfig_->Dependencies(), filePath); !dmn.empty()) { return util::UString(dmn, allocator_).View(); } // NOTE (hurton): as a last step, try resolving using the BaseUrl again without a path delimiter at the end - if (auto res = tryFormModuleName(arktsConfig_->Package(), arktsConfig_->BaseUrl()); res) { + if (auto res = + TryFormModuleName(filePath, arktsConfig_->Package(), arktsConfig_->BaseUrl(), arktsConfig_->CacheDir()); + res) { return util::UString(res.value(), allocator_).View(); }