function linkModules()

in internal/linker/index.js [410:484]


        function linkModules(package_path, m) {
            return __awaiter(this, void 0, void 0, function* () {
                const symlinkIn = package_path ?
                    path.posix.join(bin, package_path, 'node_modules') :
                    'node_modules';
                if (path.dirname(m.name)) {
                    yield mkdirp(`${symlinkIn}/${path.dirname(m.name)}`);
                }
                if (m.link) {
                    const modulePath = m.link;
                    let target;
                    if (isExecroot) {
                        target = `${startCwd}/${modulePath}`;
                    }
                    if (!isExecroot || !existsSync(target)) {
                        let runfilesPath = modulePath;
                        if (runfilesPath.startsWith(`${bin}/`)) {
                            runfilesPath = runfilesPath.slice(bin.length + 1);
                        }
                        else if (runfilesPath === bin) {
                            runfilesPath = '';
                        }
                        const externalPrefix = 'external/';
                        if (runfilesPath.startsWith(externalPrefix)) {
                            runfilesPath = runfilesPath.slice(externalPrefix.length);
                        }
                        else {
                            runfilesPath = path.posix.join(workspace, runfilesPath);
                        }
                        try {
                            target = runfiles.resolve(runfilesPath);
                            if (runfiles.manifest && modulePath.startsWith(`${bin}/`)) {
                                if (!target.match(_BAZEL_OUT_REGEX)) {
                                    const e = new Error(`could not resolve module ${runfilesPath} in output tree`);
                                    e.code = 'MODULE_NOT_FOUND';
                                    throw e;
                                }
                            }
                        }
                        catch (err) {
                            target = undefined;
                            log_verbose(`runfiles resolve failed for module '${m.name}': ${err.message}`);
                        }
                    }
                    if (target && !path.isAbsolute(target)) {
                        target = path.resolve(process.cwd(), target);
                    }
                    const symlinkFile = `${symlinkIn}/${m.name}`;
                    const stats = yield gracefulLstat(symlinkFile);
                    const isLeftOver = (stats !== null && (yield isLeftoverDirectoryFromLinker(stats, symlinkFile)));
                    if (target && (yield exists(target))) {
                        if (stats !== null && isLeftOver) {
                            yield createSymlinkAndPreserveContents(stats, symlinkFile, target);
                        }
                        else {
                            yield symlinkWithUnlink(target, symlinkFile, stats);
                        }
                    }
                    else {
                        if (!target) {
                            log_verbose(`no symlink target found for module ${m.name}`);
                        }
                        else {
                            log_verbose(`potential target ${target} does not exists for module ${m.name}`);
                        }
                        if (isLeftOver) {
                            yield unlink(symlinkFile);
                        }
                    }
                }
                if (m.children) {
                    yield Promise.all(m.children.map(m => linkModules(package_path, m)));
                }
            });
        }