Repository navigation
Description
Summary
Indexer.index() creates one globalSymbols map and hands it to every document's TreeVisitor. The map is keyed by decl.node.id. For a name that a module re-exports with from x import *, pyright's alias declaration points at the shared ImportFrom statement, so every name brought in by that statement has the same key. The first name resolved through it fixes the symbol for all the others, for the rest of the run.
As a result:
- Some symbols are wrong.
os.path.join,os.path.existsand otheros.pathfunctions are emitted asposixpath/abspath().onceabspathhas been resolved. This also happens within a single file. - Output depends on which files are indexed and in what order. The same reference in the same file gets a different symbol depending on whether another file was indexed before it.
Versions
@sourcegraph/scip-python0.6.6 (also reproduced on 0.6.8)- bundled
pyright-internal1.1.301 - Node v24.22.1, macOS arm64;
python33.13 onPATH(pyright logs "unsupported" and the stdlib symbols come out as 3.11)
Repro
proj/a.py
import os.path
def absolute(p: str) -> str:
return os.path.abspath(p)proj/b.py
import os.path
def joined(a: str, b: str) -> str:
return os.path.join(a, b)proj/c.py
import os.path
os.path.abspath("x")
os.path.join("a", "b")
os.path.exists("y")repro.sh selects the file set and order with a generated pyrightconfig.json include list:
#!/bin/sh
set -eu
cd "$(dirname "$0")"
here=$(pwd)
echo '[]' > env.json # empty package list: no pip probing
run() { # $1 = output name, $2 = pyright "include" list (file set and order)
printf '{"include": %s}\n' "$2" > proj/pyrightconfig.json
(cd proj && npx --yes @sourcegraph/scip-python@0.6.6 index \
--project-name repro --project-version 0.1 \
--environment "$here/env.json" --output "$here/$1.scip" --quiet)
rm proj/pyrightconfig.json
}
run b-only '["b.py"]'
run b-then-a '["b.py", "a.py"]'
run a-then-b '["a.py", "b.py"]'
run c-only '["c.py"]'
for r in b-only b-then-a a-then-b; do
echo "== $r: b.py"
python3 dump.py "$r.scip" b.py | grep -E '^b.py:4:20'
done
echo "== c-only: c.py"
python3 dump.py c-only.scip c.py | grep -E ':(3|4|5):9'dump.py is a stdlib-only decoder that prints path:line:col def|ref symbol for each occurrence in one document. scip print shows the same thing.
dump.py
"""Print every occurrence in one document of a SCIP index (stdlib only).
usage: python3 dump.py index.scip relative/path.py"""
import sys
def varint(b, i):
r = s = 0
while True:
c = b[i]; i += 1
r |= (c & 0x7F) << s; s += 7
if c < 0x80:
return r, i
def fields(b):
i = 0
while i < len(b):
k, i = varint(b, i)
f, w = k >> 3, k & 7
if w == 0:
v, i = varint(b, i)
elif w == 2:
n, i = varint(b, i); v = b[i:i + n]; i += n
elif w == 1:
v = b[i:i + 8]; i += 8
elif w == 5:
v = b[i:i + 4]; i += 4
else:
raise ValueError(w)
yield f, w, v
def packed(b):
out, i = [], 0
while i < len(b):
v, i = varint(b, i); out.append(v)
return out
data = open(sys.argv[1], "rb").read()
want = sys.argv[2]
for f, _, doc in fields(data):
if f != 2: # Index.documents
continue
path = next((v.decode() for g, _, v in fields(doc) if g == 1), None)
if path != want:
continue
for g, _, occ in fields(doc):
if g != 2: # Document.occurrences
continue
rng, sym, roles = [], "", 0
for h, w, v in fields(occ):
if h == 1: rng = packed(v) if w == 2 else rng + [v]
elif h == 2: sym = v.decode()
elif h == 3: roles = v
line, col = rng[0] + 1, rng[1] + 1
print(f"{path}:{line}:{col}\t{'def' if roles & 1 else 'ref'}\t{sym}")Expected
b.py's join reference has the same symbol in every run, and in c.py each call has its own symbol:
== b-only: b.py ... posixpath/join().
== b-then-a: b.py ... posixpath/join().
== a-then-b: b.py ... posixpath/join().
== c-only: c.py
c.py:3:9 ... posixpath/abspath().
c.py:4:9 ... posixpath/join().
c.py:5:9 ... genericpath/exists(). (what a file using only os.path.exists gets)
Actual
== b-only: b.py
b.py:4:20 ref scip-python python python-stdlib 3.11 posixpath/join().
== b-then-a: b.py
b.py:4:20 ref scip-python python python-stdlib 3.11 posixpath/join().
== a-then-b: b.py
b.py:4:20 ref scip-python python python-stdlib 3.11 posixpath/abspath().
== c-only: c.py
c.py:3:9 ref scip-python python python-stdlib 3.11 posixpath/abspath().
c.py:4:9 ref scip-python python python-stdlib 3.11 posixpath/abspath().
c.py:5:9 ref scip-python python python-stdlib 3.11 posixpath/abspath().
b.py is byte-identical in every run. Only the presence and order of a.py changes the symbol of its join reference.
It isn't specific to the stdlib. A project-local re-export behaves the same way:
# lib/impl.py
def first() -> int: return 1
def second() -> int: return 2
# lib/api.py
from lib.impl import *
# use.py
from lib import api
api.first() # `lib.impl`/first().
api.second() # `lib.impl`/first(). <- expected `lib.impl`/second().Cause
In packages/pyright-scip/src/ (line numbers at tag v0.6.6):
indexer.ts,Indexer.index():const globalSymbols = new Map();(L173) is created once and passed to every document'sTreeVisitor(L248).treeVisitor.ts,rawGetLsifSymbol/rawSetLsifSymbol(L883–893): any non-local symbol is stored inglobalSymbolsundernode.id.treeVisitor.ts,emitDeclaration: on an alias declaration, the resolved target's symbol is cached under the declaration's node,this.rawSetLsifSymbol(declNode, symbol, symbol.isLocal())(L701;pushTypeReferencedoes the same at L1374). Every later lookup,this.rawGetLsifSymbol(decl.node)at L569, returns that cached value first.- In pyright's binder (
packages/pyright-internal/src/analyzer/binder.ts,visitImportFrom, theisWildcardImportbranch), each name a wildcard import brings in gets anAliasDeclarationwhosenodeis theImportFromstatement itself. OnlysymbolNametells the names apart.
So for typeshed's os/path.pyi (from posixpath import *), abspath, join and exists all share one cache key: the id of that ImportFrom node. Whichever name is resolved first in the run wins. Because the map lives for the whole run, the winner is decided by whichever document resolves one of these names first. That makes it depend on the file set and the include order, not just on the document being emitted.
The same code is on the default branch (scip, at 74d73bd): indexer.ts L189/L264 and treeVisitor.ts L550, L680, L863–870.
Related, but a different bug: #247 (star-imported names in the referencing module resolve to the module symbol).
Why it matters
- Wrong symbols. Cross-references for common stdlib functions (
os.path.join,os.path.exists, …), and for anything re-exported withimport *, point at an unrelated definition. - Non-deterministic output across file sets and orders. A document's symbols depend on which other documents were indexed, and in what order. A partial or incremental index (
--target-only, a subsetinclude, re-indexing only changed files) can't reproduce what a full run emits for the same document, and a small edit in one file can change the emitted symbols of other, unrelated files. We hit this in a code-intelligence tool that consumes SCIP and compares per-document output across runs.
On larger projects we also saw SymbolInformation for an inherited attribute appear in one document or not, depending on whether an earlier document had filled the shared map. We haven't reduced that case.
Suggested direction
Either of these would make a document's symbols independent of the other documents:
- Make the cache key identify the declaration, not just its node, so the cached value can't depend on which reference resolves it first. For alias declarations from a wildcard import, the key needs
symbolName, for example${decl.node.id}:${decl.symbolName}. Alternatively, cache the resolved declaration's node (resolveAliasDeclaration(decl)) rather than the importing statement, or don't cache wildcard aliases at all. - Scope the symbol-naming cache per document, keeping only data that can't vary with the resolving site shared across the run.
Option 1 alone fixes the wrong os.path.* symbols. A shared cache can stay as an optimization, as long as each value is a pure function of its key.
🤖 Generated with Claude Code
Activity
linear-code commentedon Dec 3, 2026
OSS-24240