Skip to content

fix(loader): discover stdlib via walk-up + user-share fallbacks (closes #415) - #433

Merged
hyperpolymath merged 1 commit into
mainfrom
fix/stdlib-discovery-415
May 28, 2026
Merged

fix(loader): discover stdlib via walk-up + user-share fallbacks (closes #415)#433
hyperpolymath merged 1 commit into
mainfrom
fix/stdlib-discovery-415

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

Summary

Closes #415. Idaptik PR #107 hit this directly: use prelude::{Option, Some, None} failed under affinescript check when invoked from a directory that wasn't the affinescript repo root, because the loader's default config only knew about ./stdlib and the AFFINESCRIPT_STDLIB env var. The workaround was a local enum UndoResult { ... }.

Replaces the hard-coded ./stdlib default with discover_stdlib, which tries in order:

# Path Status
1 $AFFINESCRIPT_STDLIB unchanged — explicit operator override
2 ./stdlib unchanged — in-repo dev
3 walk up from CWD looking for stdlib/prelude.affine NEW — catches sub-directory invocations
4 <binary_dir>/../share/affinescript/stdlib/ NEW — XDG-style installed location
5 $HOME/.local/share/affinescript/stdlib/ NEW — user-local install

Falls back to ./stdlib (preserves historical ModuleNotFound error) if nothing matches.

Verification (local)

Scenario Result
in-repo root ./stdlib resolves (unchanged)
in-repo sub-dir walk-up finds it (NEW)
bare /tmp/ dir, after cp -r stdlib ~/.local/share/affinescript/ user-share finds it (NEW)

What this PR does NOT do

The literal use stdlib::Option parser syntax mentioned in #415's body is a parse error in current grammar (multi-segment stdlib:: prefix not accepted). That's a grammar change deferred as follow-up. The practical Done when criterion — affinescript check succeeds with use prelude::{Option, Some, None} from any directory — is met by this PR.

Roadmap updates

🤖 Generated with Claude Code

#415)

Issue #415: `affinescript check file.affine` failed to resolve
`use prelude::{Option, Some, None}` outside the repo root because the
loader's default config only knew about `./stdlib` and the
`AFFINESCRIPT_STDLIB` env var. Idaptik PR #107 hit this directly and
had to define a local `enum UndoResult { ... }` as a workaround.

Replaces the hard-coded `./stdlib` default with `discover_stdlib`,
which tries in order:

1. `$AFFINESCRIPT_STDLIB` — explicit operator override (unchanged)
2. `./stdlib` — current behaviour, in-repo dev (unchanged)
3. **NEW**: walk up from CWD looking for `stdlib/prelude.affine` —
   catches `affinescript check` runs from any sub-directory of an
   affinescript repo
4. **NEW**: `<binary_dir>/../share/affinescript/stdlib/` — XDG-style
   alongside an installed binary (`opam install`, `make install`, etc.)
5. **NEW**: `$HOME/.local/share/affinescript/stdlib/` — user-local
   install via `cp -r stdlib/ ~/.local/share/affinescript/stdlib/`

Falls back to `./stdlib` (preserving the historical
`ModuleNotFound` surface) if nothing matches, so misconfigured
environments still see a clean error rather than a silent default.

Verified locally with three scenarios:
  in-repo root      → `./stdlib` resolves (unchanged)
  in-repo sub-dir   → walk-up finds `stdlib/prelude.affine` (NEW)
  bare /tmp/ dir    → `~/.local/share/.../stdlib/` finds it (NEW,
                      after one-time `cp` install)

Does NOT close the literal `use stdlib::Option` parser syntax
mentioned in #415 (multi-segment `stdlib::` prefix is still a parse
error); that's a grammar change deferred as follow-up. The
`Done when` criterion that matters in practice — `affinescript check`
succeeds with `use prelude::{Option, Some, None}` from any directory
— is met by this PR.

Roadmap rows updated to ●:
* stdlib roadmap #5 (cross-file `use` resolvability) — `◑` → `●`

#6 (Prelude Option reach) and #10 (exhaustive imported-enum match)
remain `◑` — they depend on the pattern-compiler change rather than
the loader fix.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@hyperpolymath
hyperpolymath merged commit 079d407 into main May 28, 2026
22 of 23 checks passed
@hyperpolymath
hyperpolymath deleted the fix/stdlib-discovery-415 branch May 28, 2026 12:54
@github-actions

Copy link
Copy Markdown

🔍 Hypatia Security Scan

Findings: 81 issues detected

Severity Count
🔴 Critical 4
🟠 High 10
🟡 Medium 67

⚠️ Action Required: Critical security issues found!

View findings
[
  {
    "reason": "Action actions/checkout@v6 needs attention",
    "type": "unpinned_action",
    "file": "publish-jsr.yml",
    "action": "pin_sha",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Action denoland/setup-deno@v2 needs attention",
    "type": "unpinned_action",
    "file": "publish-jsr.yml",
    "action": "pin_sha",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in affine-vscode-publish.yml",
    "type": "unknown",
    "file": "affine-vscode-publish.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in casket-pages.yml",
    "type": "unknown",
    "file": "casket-pages.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in casket-pages.yml",
    "type": "unknown",
    "file": "casket-pages.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in ci.yml",
    "type": "unknown",
    "file": "ci.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in ci.yml",
    "type": "unknown",
    "file": "ci.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in ci.yml",
    "type": "unknown",
    "file": "ci.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in ci.yml",
    "type": "unknown",
    "file": "ci.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in ci.yml",
    "type": "unknown",
    "file": "ci.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  }
]

Powered by Hypatia Neurosymbolic CI/CD Intelligence

hyperpolymath added a commit that referenced this pull request Jun 1, 2026
…efs #239, #242) (#504)

Closes the smallest, safest tractable gaps that the four merged STEP-2
TS→AffineScript ports — phronesis#19, session-sentinel#25,
tropical-resource-typing#15, nafa-app#23 — flagged for STEP 3 of the
estate-wide campaign (`hyperpolymath/standards#239`, sub-issue `#242`).

## What lands

### `stdlib/Deno.affine` (+6 externs)

| extern | lowers to | gap |
|---|---|---|
| `statIsFile(path) -> Bool` | `Deno.statSync(path).isFile` | 3 |
| `statIsDirectory(path) -> Bool` | `Deno.statSync(path).isDirectory` |
3 |
| `bytesLength(b) -> Int` | `(b).length` | 4 |
| `bytesByteAt(b, i) -> Int` | `(b)[i]` | 4 |
| `bytesAsciiSlice(b, s, e) -> String` |
`String.fromCharCode(...(b).slice(s, e))` | 4 |
| `importMetaUrl() -> String` | `import.meta.url` | 5 |

The `bytes*` accessors give AffineScript first-class read access to the
opaque `Bytes` returned by `readFileBytes` so callers can peek at
file-magic / fixed-width binary headers without round-tripping through
`readTextFile` (which throws on binary). `importMetaUrl` exposes the JS
`__dirname` / `__filename` idiom for "find my own location"; only legal
at module top level, which the Deno-ESM backend's output already is.

### `stdlib/string.affine` (1 char)

`fn ends_with` → `pub fn ends_with`. The function existed but was
private, so the four STEP-2 ports each inlined a `string_sub`-backed
helper instead of importing it. Gap 2.

### `lib/codegen_deno.ml` — wildcard `let _` fix (gap 7)

`StmtLet { sl_pat = PatWildcard _; … }` now lowers to a bare expression
statement instead of `const _ = X;`. Three back-to-back `let _ = side()`
discards in the same scope tripped JS `SyntaxError: Identifier '_' has
already been declared`. The wildcard pattern doesn't bind, so dropping
the binding entirely preserves AffineScript semantics (evaluate for side
effects).

```js
// before
function discard_chain() {
  const _ = side(1);
  const _ = side(2);   // ❌ SyntaxError
  const _ = side(3);
  return 42;
}

// after
function discard_chain() {
  side(1);
  side(2);
  side(3);
  return 42;
}
```

### Tests

`tests/codegen-deno/deno_scripting_part2.{affine,deno.js,harness.mjs}` —
11 assertions across the new lowerings + the wildcard fix. The harness
reaching its discard-chain assertion is itself the test for gap 7: a
syntax-error generated module would have thrown at the dynamic `import`
line before any assert ran.

## Verification

| step | result |
|---|---|
| `dune build bin/main.exe` | ✅ |
| `dune runtest` | ✅ 353/353 |
| `./tools/run_codegen_deno_tests.sh` | ✅ all harnesses (incl. the new
one) |

## What does not land here (deferred deliberately)

- **gap 1** — `Deno.test` extern lowering. The four merged STEP-2 ports
use a panic-on-fail `main()` driver and it works; not unblocking STEP 4.
- **gap 6** — native TOML parser. Heavier work; STEP-2 ports used regex
field-presence checks where TOML mattered.

Gaps 9 (`AFFINESCRIPT_STDLIB` env discovery ladder, shipped #433) and 10
(negative integer literals, verified working in all contexts — match
arms, array literals, equality, ternaries) were audited as
already-resolved.

## Refs

- Closes 6 of the 10 gaps blocking `hyperpolymath/standards#242` (STEP 3
of `#239`).
- Unblocks STEP 4 (`#243`, mid-tier 4-9 file ports) and STEP 5 (`#244`,
idaptik DLC) for any consumer that needs stat predicates, byte
accessors, module-URL inspection, the public `ends_with`, or multiple
`let _` discards in a single scope.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Jonathan D.A. Jewell <6759885+hyperpolymath@users.noreply.github.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant