Commit b70cfc8a
b70cfc8adaacMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Give unsupported require "lib" a clear "not available" diagnostic
A require "X" with no bundled lib/X.rb already warned, but the message
("could not be resolved (no X.rb in ); the call is ignored") read
as an internal resolution failure and fired even for libs Spinel
provides natively without a .rb (e.g. JSON), so it was both noisy and
misleading.
- Add a small native-lib allowlist (currently just
json) checked in resolve_plain_requires: a require of a natively-provided module is a harmless no-op and no longer warns. Bundled .rb libs (set, stringio, strscan, erb, forwardable, optparse) resolve by file existence and never reach this branch. - Everything else gets a definitive message:
warning: 'tempfile' is not available in Spinel; the require is ignored and code using it will fail
The require is still ignored either way (Spinel can't supply the lib); this just tells the user plainly which libraries aren't available.
Fixes #1133 Fixes #1134 Fixes #1143 Fixes #1144
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- b70cfc8adaac
- Parent
- e589bffcbaed
spinel_parse.cmodifiedView file
@@ -1565,6 +1565,20 @@
15651565
return result;15661566
}15671567
1568+
/* A `require "X"` whose X has no bundled lib/X.rb may still be provided1569+
natively by the Spinel runtime/codegen (e.g. the JSON module). Such a1570+
require is a harmless no-op and must not warn. Bundled .rb libs (set,1571+
stringio, strscan, erb, ...) are resolved by file existence and never1572+
reach the not-found branch, so they don't belong here -- only the1573+
C-native modules that map to a stdlib require name do. */1574+
static int sp_lib_is_native(const char *name) {1575+
static const char *const natives[] = { "json", NULL };1576+
for (int i = 0; natives[i]; i++) {1577+
if (strcmp(name, natives[i]) == 0) return 1;1578+
}1579+
return 0;1580+
}1581+
15681582
/* ---- Plain require resolution ---- */15691583
static char *resolve_plain_requires(char *source, const char *exe_path) {15701584
/* Find lib/ directory relative to this executable */@@ -1618,10 +1632,16 @@
16181632
free(canonical);16191633
content = read_file(lib_path);16201634
if (!content) {1621-
fprintf(stderr,1622-
"warning: require \"%s\" could not be resolved (no %s.rb in %s); the call is ignored\n",1623-
lib_name, lib_name, lib_dir);1624-
content = strdup("# require not resolved");1635+
if (sp_lib_is_native(lib_name)) {1636+
/* Provided natively by the Spinel runtime; the require is a1637+
harmless no-op, so don't warn. */1638+
content = strdup("# require provided by Spinel runtime");1639+
} else {1640+
fprintf(stderr,1641+
"warning: '%s' is not available in Spinel; the require is ignored and code using it will fail\n",1642+
lib_name);1643+
content = strdup("# require not resolved");1644+
}16251645
} else {16261646
char *resolved = resolve_requires(content, lib_path);16271647
free(content);test/require_unsupported_lib.rbaddedView file
Loading diff…
test/require_unsupported_lib.rb.expectedaddedView file
Loading diff…