-
Notifications
You must be signed in to change notification settings - Fork 12
test: guard the public feature graph #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jderochervlk
merged 2 commits into
codex/option5-helper-cleanup
from
codex/option5-feature-guardrails
Aug 7, 2026
+234
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,40 @@ Generated implementation modules such as `DomTypes`, `FetchTypes`, `EventTypes`, | |
| module's `t` type when it has one, or use a dedicated public type module. Otherwise, let | ||
| the value type be inferred from constructors and accessors. | ||
|
|
||
| ## Consumer feature bundles | ||
|
|
||
| Feature names select related bindings and their transitive dependencies. They do not add | ||
| another namespace segment. For example, selecting `WebAPI.Fetch` enables flat modules such | ||
| as `WebAPI.Fetch`, `WebAPI.Request`, `WebAPI.Response`, and `WebAPI.Headers`. | ||
|
|
||
| ```json | ||
| { | ||
| "dependencies": [ | ||
| { | ||
| "name": "@rescript/webapi", | ||
| "features": ["WebAPI.Fetch", "WebAPI.HTML"] | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| The supported feature bundles are: | ||
|
|
||
| ```text | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe worth a table? |
||
| WebAPI.DOM WebAPI.Event WebAPI.DOMPlatform | ||
| WebAPI.DOMNodes WebAPI.File WebAPI.HTML | ||
| WebAPI.Window WebAPI.CSSOM WebAPI.CSSFontLoading | ||
| WebAPI.Geometry WebAPI.SVG WebAPI.Animation | ||
| WebAPI.Device WebAPI.Navigator WebAPI.Canvas | ||
| WebAPI.URL WebAPI.Fetch WebAPI.UIEvents | ||
| WebAPI.Observers WebAPI.Media WebAPI.WebAudio | ||
| WebAPI.Storage WebAPI.Messaging WebAPI.Workers | ||
| WebAPI.Crypto WebAPI.Performance WebAPI.ViewTransitions | ||
| ``` | ||
|
|
||
| The package owns each bundle as one internal source folder. Those internal folder feature | ||
| names are an implementation detail; consumers should use only the qualified names above. | ||
|
|
||
| ## Fetch | ||
|
|
||
| Use `WebAPI.Fetch.fetch` for string URLs and `WebAPI.Fetch.fetchWithRequest` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import { existsSync, readFileSync } from "node:fs"; | ||
| import path from "node:path"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); | ||
| const configPath = path.join(repoRoot, "rescript.json"); | ||
|
|
||
| const expectedFeatureOwners = new Map([ | ||
| ["WebAPI.DOM", "DOM"], | ||
| ["WebAPI.Event", "Event"], | ||
| ["WebAPI.DOMPlatform", "DOMPlatform"], | ||
| ["WebAPI.DOMNodes", "DOMNodes"], | ||
| ["WebAPI.File", "File"], | ||
| ["WebAPI.HTML", "HTML"], | ||
| ["WebAPI.Window", "Window"], | ||
| ["WebAPI.CSSOM", "CSSOM"], | ||
| ["WebAPI.CSSFontLoading", "CSSFontLoading"], | ||
| ["WebAPI.Geometry", "Geometry"], | ||
| ["WebAPI.SVG", "SVG"], | ||
| ["WebAPI.Animation", "Animation"], | ||
| ["WebAPI.Device", "Device"], | ||
| ["WebAPI.Navigator", "Navigator"], | ||
| ["WebAPI.Canvas", "Canvas"], | ||
| ["WebAPI.URL", "URL"], | ||
| ["WebAPI.Fetch", "Fetch"], | ||
| ["WebAPI.UIEvents", "UIEvents"], | ||
| ["WebAPI.Observers", "Observers"], | ||
| ["WebAPI.Media", "Media"], | ||
| ["WebAPI.WebAudio", "WebAudio"], | ||
| ["WebAPI.Storage", "Storage"], | ||
| ["WebAPI.Messaging", "Messaging"], | ||
| ["WebAPI.Workers", "Workers"], | ||
| ["WebAPI.Crypto", "Crypto"], | ||
| ["WebAPI.Performance", "Performance"], | ||
| ["WebAPI.ViewTransitions", "ViewTransitions"], | ||
| ]); | ||
|
|
||
| const uniqueDuplicates = (values) => [ | ||
| ...new Set(values.filter((value, index) => values.indexOf(value) !== index)), | ||
| ]; | ||
|
|
||
| const sameMembers = (left, right) => | ||
| left.length === right.length && left.every((value) => right.includes(value)); | ||
|
|
||
| const readConfig = () => { | ||
| try { | ||
| return { _tag: "Success", value: JSON.parse(readFileSync(configPath, "utf8")) }; | ||
| } catch (error) { | ||
| return { | ||
| _tag: "Failure", | ||
| message: error instanceof Error ? error.message : String(error), | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| const validateFeatureNames = (featureEntries) => { | ||
| const actualNames = featureEntries.map(([name]) => name); | ||
| const expectedNames = [...expectedFeatureOwners.keys()]; | ||
|
|
||
| return sameMembers(actualNames, expectedNames) | ||
| ? [] | ||
| : [ | ||
| `Expected exactly these ${expectedNames.length} public features:\n${expectedNames.join("\n")}\n\nReceived:\n${actualNames.join("\n")}`, | ||
| ]; | ||
| }; | ||
|
|
||
| const validateSources = (sourceEntries) => { | ||
| const sourceFeatures = sourceEntries.map((source) => source.feature); | ||
| const expectedInternalFeatures = [...expectedFeatureOwners.values()]; | ||
| const duplicateFeatures = uniqueDuplicates(sourceFeatures); | ||
| const qualifiedFeatures = sourceFeatures.filter((feature) => feature.startsWith("WebAPI.")); | ||
| const missingDirectories = sourceEntries | ||
| .filter((source) => !existsSync(path.join(repoRoot, source.dir))) | ||
| .map((source) => source.dir); | ||
|
|
||
| return [ | ||
| ...(sameMembers(sourceFeatures, expectedInternalFeatures) | ||
| ? [] | ||
| : ["Source features do not match the 27 expected internal folder features."]), | ||
| ...(duplicateFeatures.length === 0 | ||
| ? [] | ||
| : [`Duplicate source features: ${duplicateFeatures.join(", ")}`]), | ||
| ...(qualifiedFeatures.length === 0 | ||
| ? [] | ||
| : [`Source features must be unqualified: ${qualifiedFeatures.join(", ")}`]), | ||
| ...(missingDirectories.length === 0 | ||
| ? [] | ||
| : [`Missing source directories: ${missingDirectories.join(", ")}`]), | ||
| ]; | ||
| }; | ||
|
|
||
| const validateFeatureOwners = (featureEntries, sourceEntries) => { | ||
| const internalFeatures = new Set(sourceEntries.map((source) => source.feature)); | ||
|
|
||
| return featureEntries.flatMap(([featureName, expansion]) => { | ||
| if (!Array.isArray(expansion)) { | ||
| return [`${featureName} must expand to an array.`]; | ||
| } | ||
|
|
||
| const directInternalFeatures = expansion.filter((feature) => internalFeatures.has(feature)); | ||
| const expectedOwner = expectedFeatureOwners.get(featureName); | ||
|
|
||
| return directInternalFeatures.length === 1 && directInternalFeatures[0] === expectedOwner | ||
| ? [] | ||
| : [ | ||
| `${featureName} must directly include only its owning internal feature ${expectedOwner}; received ${directInternalFeatures.join(", ") || "none"}.`, | ||
| ]; | ||
| }); | ||
| }; | ||
|
|
||
| const validatePublicModules = (sourceEntries) => { | ||
| const publicModules = sourceEntries.flatMap((source) => | ||
| (source.public ?? []).map((moduleName) => ({ moduleName, sourceDir: source.dir })), | ||
| ); | ||
| const duplicateModules = uniqueDuplicates(publicModules.map(({ moduleName }) => moduleName)); | ||
| const missingModules = publicModules | ||
| .filter( | ||
| ({ moduleName, sourceDir }) => | ||
| !existsSync(path.join(repoRoot, sourceDir, `${moduleName}.res`)), | ||
| ) | ||
| .map(({ moduleName, sourceDir }) => `${sourceDir}/${moduleName}.res`); | ||
|
|
||
| return [ | ||
| ...(duplicateModules.length === 0 | ||
| ? [] | ||
| : [`Duplicate public modules: ${duplicateModules.join(", ")}`]), | ||
| ...(missingModules.length === 0 | ||
| ? [] | ||
| : [`Missing public module files: ${missingModules.join(", ")}`]), | ||
| ]; | ||
| }; | ||
|
|
||
| const validateConfig = (config) => { | ||
| const featureEntries = Object.entries(config.features ?? {}); | ||
| const sourceEntries = (config.sources ?? []).filter( | ||
| (source) => source !== null && typeof source === "object" && typeof source.feature === "string", | ||
| ); | ||
|
|
||
| return [ | ||
| ...validateFeatureNames(featureEntries), | ||
| ...validateSources(sourceEntries), | ||
| ...validateFeatureOwners(featureEntries, sourceEntries), | ||
| ...validatePublicModules(sourceEntries), | ||
| ]; | ||
| }; | ||
|
|
||
| const rescriptCliPath = path.join(repoRoot, "node_modules", "rescript", "cli", "rescript.js"); | ||
|
|
||
| const runRescript = (args) => | ||
| spawnSync(process.execPath, [rescriptCliPath, ...args], { | ||
| cwd: repoRoot, | ||
| encoding: "utf8", | ||
| }); | ||
|
jderochervlk marked this conversation as resolved.
|
||
|
|
||
| const formatProcessFailure = (featureName, command, result) => | ||
| [`${featureName} failed during ${command}.`, result.stdout?.trim(), result.stderr?.trim()] | ||
| .filter(Boolean) | ||
| .join("\n"); | ||
|
|
||
| const compileFeature = (featureName) => { | ||
| const cleanResult = runRescript(["clean"]); | ||
| if (cleanResult.status !== 0) { | ||
| return { _tag: "Failure", message: formatProcessFailure(featureName, "clean", cleanResult) }; | ||
| } | ||
|
|
||
| const buildResult = runRescript(["build", "--prod", "--features", featureName]); | ||
| return buildResult.status === 0 | ||
| ? { _tag: "Success" } | ||
| : { _tag: "Failure", message: formatProcessFailure(featureName, "build", buildResult) }; | ||
| }; | ||
|
|
||
| const configResult = readConfig(); | ||
| if (configResult._tag === "Failure") { | ||
| console.error(`Unable to read rescript.json: ${configResult.message}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const validationErrors = validateConfig(configResult.value); | ||
| if (validationErrors.length > 0) { | ||
| console.error(validationErrors.join("\n\n")); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| console.log(`Validated ${expectedFeatureOwners.size} public feature definitions.`); | ||
|
|
||
| for (const featureName of expectedFeatureOwners.keys()) { | ||
| const result = compileFeature(featureName); | ||
| if (result._tag === "Failure") { | ||
| console.error(result.message); | ||
| process.exit(1); | ||
| } | ||
| console.log(`[ok] ${featureName}`); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish we could go here without the WebAPI prefix.