Repository: microsoft/TypeScript
File: src/compiler/moduleSpecifiers.ts (TypeScript)

Code before:
    //   2. Specifiers generated using "paths" from tsconfig
    //   3. Non-relative specfiers resulting from a path through node_modules (e.g. "@foo/bar/path/to/file")
    //   4. Relative paths
    let nodeModulesSpecifiers: string[] | undefined;
    let pathsSpecifiers: string[] | undefined;
    let redirectPathsSpecifiers: string[] | undefined;
    let relativeSpecifiers: string[] | undefined;
    for (const modulePath of modulePaths) {
        const specifier = modulePath.isInNodeModules
            ? tryGetModuleNameAsNodeModule(modulePath, info, importingSourceFile, host, compilerOptions, userPreferences, /*packageNameOnly*/ undefined, options.overrideImportMode)
            : undefined;
        if (specifier && !(forAutoImport && isExcludedByRegex(specifier, preferences.excludeRegexes))) {
            nodeModulesSpecifiers = append(nodeModulesSpecifiers, specifier);
            if (modulePath.isRedirect) {
                // If we got a specifier for a redirect, it was a bare package specifier (e.g. "@foo/bar",
                // not "@foo/bar/path/to/file"). No other specifier will be this good, so stop looking.
                return { kind: "node_modules", moduleSpecifiers: nodeModulesSpecifiers, computedWithoutCache: true };
            }
        }

        const local = getLocalModuleSpecifier(
            modulePath.path,
            info,
            compilerOptions,
            host,
            options.overrideImportMode,
            preferences,
            /*pathsOnly*/ modulePath.isRedirect || !!specifier,
        );
        if (!local || forAutoImport && isExcludedByRegex(local, preferences.excludeRegexes)) {
            continue;
        }
        if (modulePath.isRedirect) {
            redirectPathsSpecifiers = append(redirectPathsSpecifiers, local);
        }
        else if (pathIsBareSpecifier(local)) {
            if (pathContainsNodeModules(local)) {
                // We could be in this branch due to inappropriate use of `baseUrl`, not intentional `paths`
                // usage. It's impossible to reason about where to prioritize baseUrl-generated module
                // specifiers, but if they contain `/node_modules/`, they're going to trigger a portability
                // error, so *at least* don't prioritize those.
                relativeSpecifiers = append(relativeSpecifiers, local);
            }
            else {
                pathsSpecifiers = append(pathsSpecifiers, local);
            }
        }
        else if (forAutoImport || !importedFileIsInNodeModules || modulePath.isInNodeModules) {
            // Why this extra conditional, not just an `else`? If some path to the file contained
            // 'node_modules', but we can't create a non-relative specifier (e.g. "@foo/bar/path/to/file"),
            // that means we had to go through a *sibling's* node_modules, not one we can access directly.

Review comment (bug):
This was a stray debugging change (the result of which was, removing this causes no test failures 🫤), revertig

Code after:
    //   2. Specifiers generated using "paths" from tsconfig
    //   3. Non-relative specfiers resulting from a path through node_modules (e.g. "@foo/bar/path/to/file")
    //   4. Relative paths
    let nodeModulesSpecifiers: string[] | undefined;
    let pathsSpecifiers: string[] | undefined;
    let redirectPathsSpecifiers: string[] | undefined;
    let relativeSpecifiers: string[] | undefined;
    for (const modulePath of modulePaths) {
        const specifier = modulePath.isInNodeModules
            ? tryGetModuleNameAsNodeModule(modulePath, info, importingSourceFile, host, compilerOptions, userPreferences, /*packageNameOnly*/ undefined, options.overrideImportMode)
            : undefined;
        if (specifier && !(forAutoImport && isExcludedByRegex(specifier, preferences.excludeRegexes))) {
            nodeModulesSpecifiers = append(nodeModulesSpecifiers, specifier);
            if (modulePath.isRedirect) {
                // If we got a specifier for a redirect, it was a bare package specifier (e.g. "@foo/bar",
                // not "@foo/bar/path/to/file"). No other specifier will be this good, so stop looking.
                return { kind: "node_modules", moduleSpecifiers: nodeModulesSpecifiers, computedWithoutCache: true };
            }
        }

        const local = getLocalModuleSpecifier(
            modulePath.path,
            info,
            compilerOptions,
            host,
            options.overrideImportMode || importingSourceFile.impliedNodeFormat,
            preferences,
            /*pathsOnly*/ modulePath.isRedirect || !!specifier,
        );
        if (!local || forAutoImport && isExcludedByRegex(local, preferences.excludeRegexes)) {
            continue;
        }
        if (modulePath.isRedirect) {
            redirectPathsSpecifiers = append(redirectPathsSpecifiers, local);
        }
        else if (pathIsBareSpecifier(local)) {
            if (pathContainsNodeModules(local)) {
                // We could be in this branch due to inappropriate use of `baseUrl`, not intentional `paths`
                // usage. It's impossible to reason about where to prioritize baseUrl-generated module
                // specifiers, but if they contain `/node_modules/`, they're going to trigger a portability
                // error, so *at least* don't prioritize those.
                relativeSpecifiers = append(relativeSpecifiers, local);
            }
            else {
                pathsSpecifiers = append(pathsSpecifiers, local);
            }
        }
        else if (forAutoImport || !importedFileIsInNodeModules || modulePath.isInNodeModules) {
            // Why this extra conditional, not just an `else`? If some path to the file contained
            // 'node_modules', but we can't create a non-relative specifier (e.g. "@foo/bar/path/to/file"),
            // that means we had to go through a *sibling's* node_modules, not one we can access directly.