Skip to content

Credit caller locals from the program definition Stata has in memory, navigate to locals a loaded program sets, and fix Find References file names, graph arguments after a quoted path, and EPIPE on early pipe close - #431

Merged
jbearak merged 1 commit into
mainfrom
t3/fix-scope-navigation-references-cli
Nov 19, 2026

Conversation

@jbearak

@jbearak jbearak commented Nov 19, 2026

Copy link
Copy Markdown
Owner

A cold review of #422 and #423 found that Sight credited caller locals from the wrong definition of a program. If a do-file that had run defined setter, a setter.ado beside it still decided which locals the call set. In Stata the reverse holds: a program already in memory is used, and the ado is loaded only when no program of that name is in memory, including after program drop (checked in StataMP 18). #423 also took away navigation for a local that a loaded program sets: the warning went away, but Go to Definition, hover and completion returned nothing. The review also found three smaller bugs in Find References, sight graph and the CLI.

Which definition a call uses (S5). One walk over the file, using the resolved execution scope, now decides which definition each call reaches: one defined earlier in the file, one from a parent, or one from a file loaded before the call. If none of those defines it, the ado named after the program is used. The walk then derives all three kinds of caller write from that definition: a literal c_local, a local() option, and a c_local of an argument. The analyzer credits a workspace program only when no scope could change the answer: the program's one definition outside the file, or, inside a program body, the file's own helper. Everything else goes to the walk. The index no longer replaces a program with its ado. It keeps the definition whose URI sorts first and lists every definition in SymbolTable.program_definitions. That field is a small addition to src/types/index.ts, outside the directories this PR owns.

  • fixinverse: 1 issue → 0. optinverse: 1 → 0, and 0 in all 8 repeats. shadowado: 0 → 1, and it no longer offers a definition, hover or completion.
  • Order dependence: optinverse failed 8 of 8 times on main because of the ado overwrite, so that failure was deterministic. The order dependence sits underneath it: without the overwrite, "last file indexed wins" decides. One new test indexes the same workspace in both file orders. Another covers a helper name that several ados define. Both fail on main.

Navigation for locals a loaded program sets (S8). Definition, hover, completion and Find References now read the same walk as diagnostics. Wherever the warning is suppressed, the argument word (or the option argument) is the local's definition, and Find References also works from that word. navdirect: Go to Definition goes to main.do line 2, cols 7–13, hover shows the local, and completion offers it. The cases that still warn return no definition.

Find References file names (S7). save, use, type and the other file-name commands count only as a statement's command. That means first in the statement after any prefixes, or first after a one-line if condition. Workspace files are indexed without an AST, so the if split copies the parser's rule rather than using the AST. if _N > 0 regress type i.x finds i.x. if save == 1 regress y L.dta finds L.dta. In append using if.dta l.dta, l.dta is a file name. The cases from #413 and #422 still pass, and the token-count test still passes.

sight graph --json (S10). The option comma is searched for only after the whole path. do "survey, wave 1.do" 2019 → ["2019"], and do "child,part.do" x → ["x"].

Early pipe close (S11). check, parse and graph now stop writing without printing an error when the reader closes early (| head -1). They keep their documented exit codes (0 on success, 2 when a file fails) under both bun src/cli.ts and node dist/sight-server.js.

Not fixed

  • S13: two run calls on one physical line with a call between them. This did not fall out of S5: forward call sites carry only a line number. On a line that holds several loads, the earliest load end still counts for all of them.

Corpus (3,780 ados) compared with chk-final2.json (origin/main at 9f94055): 0 new diagnostics at new positions, and the result is identical across 3 runs.

  • 56 warnings at the same positions changed from undefined_macro to out_of_scope_symbol ("used before it is defined (line N)" / "defined only inside program parseit"). This is in _pss_chk_testmainopts, gsem_parse_gauss_args, gsem_parse_poisson_args and gsem_parse_st_args. Each calls a same-file helper whose name other ados also define (parseit, CheckNumVar). On main, which definition counted depended on index order: the structurally identical _pss_chk_cimainopts was already classified the new way. The file's own helper now always counts.
  • 15 removed. Each is a reference inside the call statement to such a helper (CheckNumVar ldepvar \ldepvar', GetVCE, ... `options'`). The analyzer treats the call's own arguments as after the definition. That rule is unchanged and already applied to files whose helper happened to win the merge.
  • The 69 rev2-an workspaces from Credit c_local arguments from programs that files loaded at any depth define, keep them after a later tokenize, and stop input types from defining variables #423 give the same issue counts as before (2 runs each).

Proof: the probes for fixinverse, shadowado, optinverse, navdirect, ref_normal and graphcomma were run before and after. 18 new tests fail on origin/main and pass here (plus the node EPIPE cases, which failed before the change). check:line-endings, typecheck, bun test ./tests (10284 pass), lint and test:ci all pass when run separately.

… navigate to locals a loaded program sets, and fix file names in Find References, graph arguments after a quoted path, and EPIPE on an early pipe close

- A call's c_local, local() option and c_local-of-argument writes now
  come from the definition the caller's resolved execution scope
  reaches at the call (defined earlier, by a parent, or by a file
  loaded before it); the ado named after the program counts only when
  none is, also after program drop (checked in StataMP 18). The index
  no longer overwrites a program with its ado; it keeps the definition
  whose URI sorts first and lists every definition, so results no
  longer depend on index order.
- The analyzer credits a workspace program only when no scope can
  change the answer (the one definition outside the file, or, inside a
  program body, the file's own helper); the rest is decided by one
  walk that diagnostics and navigation share.
- Go to Definition, hover, completion and Find References treat the
  argument word or option argument as the local's definition wherever
  the warning is suppressed, including through a loaded file.
- Find References reads save/use/do/type/... as a file command only in
  command position: first in a statement or right after a one-line if
  condition, split as the parser splits it. A separate if or in ends
  the file names after using; if.dta does not.
- sight graph --json looks for the option comma after the whole path,
  so do "survey, wave 1.do" 2019 passes ["2019"].
- check, parse and graph stop writing quietly when the reader closes
  early and keep their exit codes.
@coderabbitai

coderabbitai Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

Next included review available in 45 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 87 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 05e9c956-851a-4d83-bda1-618e777c42be
Commits

Reviewing files that changed from the base of the PR and between 9f94055 and fc14e5d.

Files selected for processing: 25

  • docs/cli.md
  • docs/configuration.md
  • docs/cross-file.md
  • docs/diagnostics.md
  • docs/find-references.md
  • src/analyzer/c-local-arg-positions.ts
  • src/analyzer/command-macro-writes.ts
  • src/analyzer/index.ts
  • src/analyzer/program-macro-writes.ts
  • src/cli.ts
  • src/cli/graph.ts
  • src/indexer/index.ts
  • src/providers/command-position.ts
  • src/providers/completion.ts
  • src/providers/definition.ts
  • src/providers/diagnostics.ts
  • src/providers/hover.ts
  • src/providers/program-macro-credit.ts
  • src/providers/references.ts
  • src/types/index.ts
  • tests/integration/cli-exit-drain.test.ts
  • tests/integration/program-definition-precedence.test.ts
  • tests/unit/c-local-arg-positions.test.ts
  • tests/unit/cli/parse-graph-json.test.ts
  • tests/unit/references-factor-variables.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant