wails icon indicating copy to clipboard operation
wails copied to clipboard

fix: preserve executable modes when extracting templates

Open cyphercodes opened this issue 5 months ago β€’ 2 comments

Description

Preserves executable file modes when v3 template assets are extracted. This keeps the generated Android gradlew script executable after wails init, so wails3 task android:package can run it without a manual chmod.

Fixes #5606

Type of change

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] This change requires a documentation update

How Has This Been Tested?

  • [x] Linux

Linux distro: Ubuntu 24.04-based container

  • cd v3 && go test ./internal/gosod -run 'TestExtract_StandardFilePreservesMode|TestExtract_StandardFile$' -count=1
  • cd v3 && go test ./internal/gosod -count=1
  • cd v3 && git diff --check

Test Configuration

wails doctor was not run in this container; the change is covered by focused internal/gosod unit tests.

Checklist:

  • [ ] (v2 only) I have updated website/src/pages/changelog.mdx with details of this PR (v3 changelog entries are added automatically)
  • [x] My code follows the general coding style of this project
  • [x] I have performed a self-review of my own code
  • [x] I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [x] My changes generate no new warnings
  • [x] I have added tests that prove my fix is effective or that my feature works
  • [x] New and existing unit tests pass locally with my changes

Summary by CodeRabbit

  • Bug Fixes

    • Fixed executable permissions not being preserved when extracting v3 template assets. Executable bits are now correctly restored for extracted files, including generated Android gradlew scripts.
  • Tests

    • Added regression coverage for embedded gradlew extraction and new permission-focused checks (shebang-driven executability, executable source preservation, and read-only sources staying writable), skipping Windows where applicable.

cyphercodes avatar Jun 15 '26 03:06 cyphercodes

Review Change Stack

No actionable comments were generated in the recent review. πŸŽ‰

ℹ️ Recent review info
βš™οΈ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 3e38e598-1462-4604-8a40-08f58030a4d1

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between e4d1ed72cda1c241755d52f8f5b524c01d14a510 and 5ca95eba330c7a17d7ad9fa2c15b38a7c17a3060.

πŸ“’ Files selected for processing (3)
  • v3/internal/commands/gradlew_exec_test.go
  • v3/internal/gosod/gosod.go
  • v3/internal/gosod/gosod_test.go
βœ… Files skipped from review due to trivial changes (1)
  • v3/internal/commands/gradlew_exec_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • v3/internal/gosod/gosod.go

Walkthrough

processTemplates and copyFile in gosod.go now restore executable bits using source file mode detection and shebang fallback: processTemplates calls makeExecutableIfNeeded after closing rendered template files, while copyFile uses os.OpenFile with explicit flags and then conditionally applies source executable bits. Unit tests validate shebang-based and pre-executable file preservation while ensuring read-only sources remain writable. An end-to-end regression test confirms embedded gradlew extracts executable. Changelog documents the fix for Android build failures.

Changes

Preserve executable file permissions during template extraction

Layer / File(s) Summary
gosod.go executable bit restoration in processTemplates and copyFile
v3/internal/gosod/gosod.go
processTemplates calls makeExecutableIfNeeded after closing rendered template files to recover executability from shebang detection. copyFile opens the destination with os.OpenFile using O_WRONLY|O_CREATE|O_TRUNC and mode 0666, copies source bytes via io.Copy, closes the destination, then Stat()s the source and calls makeExecutableIfNeeded(dst, srcMode) to apply executable bits either from source mode or from #! shebang detection.
gosod_test.go unit tests for executable preservation
v3/internal/gosod/gosod_test.go
runtime import added for OS-specific test skipping. Three new tests validate: TestExtract_ShebangScriptBecomesExecutable confirms shebang-only files with read-only source mode become executable after extraction, TestExtract_SourceExecBitPreserved verifies pre-executable source mode is preserved, and TestExtract_ReadOnlySourceModeStaysWritable ensures read-only source mode stays writable without gaining executable bits. All tests skip on Windows. callbackErrFileInfo method stubs reformatted with equivalent return values; alignment corrected in resolveTarget test region.
gradlew_exec_test.go end-to-end extraction test
v3/internal/commands/gradlew_exec_test.go
Adds TestEmbeddedGradlewExtractsExecutable regression test (skipped on Windows) that verifies the embedded build_assets/android/gradlew is initially non-executable, extracts the embedded android subtree to a temp directory, and asserts the extracted gradlew file becomes executable.
Changelog
v3/UNRELEASED_CHANGELOG.md
Adds a Fixed entry noting executable permissions are now preserved when extracting template assets, fixing Android gradlew scripts (referencing #5606).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

πŸ‡ Behold, the gradlew script now takes flight! processTemplates and copyFile preserve the executable right, Shebang detection and source bits align, No more permission deniedβ€”Android builds will shine! ✨

Suggested labels

reviewed βœ…

Suggested reviewers

  • leaanthony
πŸš₯ Pre-merge checks | βœ… 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (4 passed)
Check name Status Explanation
Title check βœ… Passed The title clearly and concisely summarizes the main change: preserving executable modes when extracting templates, which directly addresses the core issue.
Description check βœ… Passed The PR description provides a clear summary, references the fixed issue (#5606), documents the testing performed (Linux), and completes relevant checklist items including tests and code review.
Linked Issues check βœ… Passed The code changes fully implement the fix for #5606: executable permissions are preserved when extracting template assets, ensuring the Android gradlew script is executable after wails init.
Out of Scope Changes check βœ… Passed All changes are directly related to fixing #5606: they modify template extraction logic to preserve executable modes, add relevant tests, and update the changelog appropriately.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests

[!WARNING] There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

πŸ”§ golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jun 15 '26 03:06 coderabbitai[bot]

Addressed both review comments in e4d1ed72c:

  1. copyFile perms β€” No longer copy the source's full permission bits into os.OpenFile (which would make files read-only when the source is an embedded fs.FS reporting 0444/0555, and would silently no-op on overwrite since the perm arg is ignored for existing files). Now the file is created with 0666 so the umask applies, and only the executable bits are synced afterward via os.Chmod.

  2. Flaky mode assertion β€” TestExtract_StandardFilePreservesMode now asserts on the executable bits (&0111) instead of an exact 0755, and skips on Windows. Added TestExtract_ReadOnlySourceModeStaysWritable as a regression test confirming a read-only (0444) source extracts to a writable, non-executable file.

go test ./internal/gosod -count=1 passes; gofmt clean.

leaanthony avatar Jun 16 '26 14:06 leaanthony

Thanks for this, @cyphercodes β€” you correctly identified that embedded scripts lose their exec bit when extracted (go:embed reports them as non-executable), which broke ./gradlew.

That specific symptom (#5606) is already resolved on master: the Android Taskfile now does chmod +x ./gradlew immediately before invoking it (added as part of the mobile support work in #5562/#5602). Your approach of fixing the root cause in the gosod extractor is arguably cleaner, but since the live bug is already handled and this PR doesn't remove the now-redundant Taskfile chmods, I'm going to close it to avoid two parallel mechanisms for the same thing.

If we later find more embedded scripts hitting this (and want to drop the per-Taskfile chmods), the gosod approach here is a good reference. Really appreciate the fix and the test. πŸ™

taliesin-ai avatar Jun 29 '26 07:06 taliesin-ai