Skip to content

fix(web): enforce asset compilation at build-time and fix runtime 404s#219

Open
Jberlinsky wants to merge 2 commits into
GoogleCloudPlatform:mainfrom
Jberlinsky:fix/asset-compilation-in-source-builds
Open

fix(web): enforce asset compilation at build-time and fix runtime 404s#219
Jberlinsky wants to merge 2 commits into
GoogleCloudPlatform:mainfrom
Jberlinsky:fix/asset-compilation-in-source-builds

Conversation

@Jberlinsky
Copy link
Copy Markdown
Contributor

This PR ensures that Scion binaries built from source have correctly compiled web assets embedded, or fail at compile-time with a helpful error message. It also fixes a runtime bug where the server would return a 404 error if the web directory was present but empty.

Key Changes

  • Compile-Time Enforcement: Added a sentinel //go:embed directive in web/embed.go for assets/main.js. Building the project now fails immediately if the web assets haven't been built (make web), preventing the creation of "broken" binaries.
  • Improved Runtime Asset Detection: Modified hasWebAssets() in pkg/hub/web.go to verify the presence of assets/main.js before attempting to serve the SPA shell. This allows the server to gracefully fall back to a "Web UI Not Available" page even if an empty assets directory is provided via --web-assets-dir.
  • Streamlined Onboarding: Updated the installation documentation (install.md) to recommend make all, which handles both the web build and installation to ~/.local/bin in a single command.
  • Test Setup: Updated pkg/hub/web_test.go to automatically provision dummy assets for web tests, ensuring CI remains green even when running without a full frontend build (e.g., using -tags no_embed_web).

Why this is needed

Previously, because web/dist/client contained a .gitkeep file, the Go compiler would successfully embed an effectively empty directory. At runtime, the server would find this directory, serve the main HTML shell, and then fail to load the actual JavaScript, leading to a confusing 404 error (visible only in the browser console) and a blank page for users building from source.

Testing Performed

  • Verified make build fails when assets are missing.
  • Verified make all successfully builds and installs a functional binary.
  • Verified go build -tags no_embed_web still works for CLI-only builds.
  • Verified all pkg/hub tests pass.

- Use a sentinel //go:embed for assets/main.js to force compile-time failure if UI is not built
- Update hasWebAssets() to verify entry point existence, preventing 404s on empty asset dirs
- Update install.md to recommend 'make all' for automated web build and installation
- Fix web_test.go to provision dummy assets for stable test execution
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the web asset verification process by introducing a compile-time sentinel to ensure assets are built before embedding and updating the hasWebAssets check to verify the presence of the core entry point. Documentation and tests have been updated to reflect these changes. Review feedback highlights a misleading instruction in the installation guide regarding CLI-only builds and recommends adding error handling for file operations within the test suite.

Comment thread docs-site/src/content/docs/getting-started/install.md Outdated
Comment thread pkg/hub/web_test.go
Comment on lines +49 to +50
os.MkdirAll(assetsDir, 0755)
os.WriteFile(filepath.Join(assetsDir, "main.js"), []byte("// test"), 0644)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Error handling is missing for these file operations. Using require.NoError ensures the test fails immediately with a clear message if the environment setup fails.

Suggested change
os.MkdirAll(assetsDir, 0755)
os.WriteFile(filepath.Join(assetsDir, "main.js"), []byte("// test"), 0644)
require.NoError(t, os.MkdirAll(assetsDir, 0755))
require.NoError(t, os.WriteFile(filepath.Join(assetsDir, "main.js"), []byte("// test"), 0644))

Comment thread pkg/hub/web_test.go
Comment on lines +301 to +302
os.MkdirAll(assetsDir, 0755)
os.WriteFile(filepath.Join(assetsDir, "main.js"), []byte("// test"), 0644)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Error handling is missing for these file operations. Using require.NoError ensures the test fails immediately with a clear message if the environment setup fails.

Suggested change
os.MkdirAll(assetsDir, 0755)
os.WriteFile(filepath.Join(assetsDir, "main.js"), []byte("// test"), 0644)
require.NoError(t, os.MkdirAll(assetsDir, 0755))
require.NoError(t, os.WriteFile(filepath.Join(assetsDir, "main.js"), []byte("// test"), 0644))

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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