Skip to content
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

[native_assets_cli] Make the InputBuilder API less error-prone #2059

Open
dcharkes opened this issue Mar 3, 2025 · 0 comments
Open

[native_assets_cli] Make the InputBuilder API less error-prone #2059

dcharkes opened this issue Mar 3, 2025 · 0 comments

Comments

@dcharkes
Copy link
Collaborator

dcharkes commented Mar 3, 2025

In the following code one of the setup calls is missing.

final buildInputBuilder =
BuildInputBuilder()
..setupShared(
packageName: name,
packageRoot: tempUri,
outputFile: tempUri.resolve('output.json'),
outputDirectory: tempUri,
outputDirectoryShared: tempUri2,
)
..config.setupBuild(linkingEnabled: false)
..config.setupShared(buildAssetTypes: [CodeAsset.type])
..config.setupCode(
targetOS: OS.macOS,
targetArchitecture: target,
linkModePreference:
linkMode == DynamicLoadingBundled()
? LinkModePreference.dynamic
: LinkModePreference.static,
cCompiler: cCompiler,
macOS: MacOSCodeConfig(targetVersion: defaultMacOSVersion),
);

This doesn't lead to any errors, as the field in particular isn't used in the hook.

However, in general, we should try to structure the API in a way that helps developers are less likely to forget to setup a part of the input.

Instead of having separate setup methods for each individual part, only extensions should have a separate call. Something along the lines of:

BuildInputBuilder()
  ..setup(
    packageName: name,
    packageRoot: tempUri,
    outputFile: tempUri.resolve('output.json'),
    outputDirectory: tempUri,
    outputDirectoryShared: tempUri2,
    config: Config(
      buildAssetTypes: [CodeAsset.type]
      linkingEnabled: false,
    ),
  )
  ..config.setupCode(
    targetOS: OS.macOS,
    targetArchitecture: target,
    linkModePreference:
        linkMode == DynamicLoadingBundled()
            ? LinkModePreference.dynamic
            : LinkModePreference.static,
    cCompiler: cCompiler,
    macOS: MacOSCodeConfig(targetVersion: defaultMacOSVersion),
  );

It would also be better to be explicit about what extensions can add to the config. Possibly along the lines of passing a void Function(BuildConfigBuilder config) for populating the extension data instead of giving generic access to the BuildInputBuilder and a .config.

BuildInputBuilder(
  packageName: name,
  packageRoot: tempUri,
  outputFile: tempUri.resolve('output.json'),
  outputDirectory: tempUri,
  outputDirectoryShared: tempUri2,
  config: Config(
    buildAssetTypes: [CodeAsset.type]
    linkingEnabled: false,
  ),
  extensions: (config) => config.setupCode(
    targetOS: OS.macOS,
    targetArchitecture: target,
    linkModePreference:
        linkMode == DynamicLoadingBundled()
            ? LinkModePreference.dynamic
            : LinkModePreference.static,
    cCompiler: cCompiler,
    macOS: MacOSCodeConfig(targetVersion: defaultMacOSVersion),
  ),
);

(This would make the API not symmetric between base and extensions, the extension setup has to happen in a callback.)

Once we remove the non-shared outputDirectory (#2058) there is also no need in our own code to setup parts of the input.json before setting up other parts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

1 participant