Skip to content

TypeScript path aliases (@/, @common/) not resolved — imports treated as npm packages #575

Closed

Description

@kurellajunior

Problem

When a project uses TypeScript path aliases (e.g. @/ → src/, configured in tsconfig.json or vite.config.ts), graphify's AST extractor treats aliased imports as bare npm package names and falls back to taking only the last path segment:

# extract.py, _import_js()
else:
    # Bare/scoped import (node_modules) - use last segment; dropped as external
    module_name = raw.split("/")[-1]
    tgt_nid = _make_id(module_name)

So import Foo from "@/components/Foo.vue" produces an edge pointing to node ID foo_vue instead of src_components_foo_vue. Since no node with that ID exists, the edge is effectively dangling and never connects to the actual file node.

Impact

In a Vue 3 + Vite project with @/ aliasing src/, 92 of 95 Storybook story files had zero connections to their component implementation files. The graph looked like stories were isolated islands, which made community detection and cross-file queries useless for that entire layer of the codebase.

Proposed fix

Add an optional alias_map: dict[str, str] parameter to extract() and resolve it inside _import_js() before the bare-import fallback:

def _import_js(node, source, file_nid, stem, edges, str_path, alias_map=None):
    ...
    if raw.startswith("."):
        # existing relative resolution
    elif alias_map:
        for prefix, root in alias_map.items():
            if raw.startswith(prefix):
                suffix = raw[len(prefix):]
                resolved = Path(root) / suffix
                tgt_nid = _make_id(str(resolved))
                break
        else:
            module_name = raw.split("/")[-1]
            tgt_nid = _make_id(module_name)
    else:
        module_name = raw.split("/")[-1]
        tgt_nid = _make_id(module_name)

The alias map could be populated automatically by reading tsconfig.json compilerOptions.paths and/or vite.config.ts resolve.alias before calling extract(). Alternatively, a graphify.toml config file in the project root could let users declare aliases explicitly.

Workaround

For now I'm running a post-processing script after every /graphify and --update that injects the missing edges directly into graph.json. It works but requires the alias map to be maintained separately from the project's existing tsconfig.json.

Activity

  1. safishamsi commented on Apr 27, 2026

    @safishamsi
    Member

    Fixed: _import_js() now reads the nearest tsconfig.json compilerOptions.paths (cached per tsconfig file) and resolves path aliases (e.g. @/) to real paths before falling back to the bare-package branch.

  2. kurellajunior commented on Apr 28, 2026

    @kurellajunior
    Author

    Wohaho… /me watching the implementation train run by

    Do you guys do CD or how do I get this version running?

    It works in v0.5.2

    You are awesome and greetings from my Claude to your Claude 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions