swiftlang / swift-package-manager

The Package Manager for the Swift Programming Language
Apache License 2.0
9.76k stars 1.35k forks source link

The prebuild plugin does not run on every build which is expected by the documentation. #5476

Open DimaRU opened 2 years ago

DimaRU commented 2 years ago

Preface

I am trying to use the new amazing feature of Swift 5.6 - prebuild plugin and have encountered a flaw in the plugin execution. Maybe I don’t understand how the plugin works.

I used the plugin to auto-generate the build version from the GIT repository information.

From prebuild plugin documentation:
"Commands of type .prebuildCommand run before the start of every build.":
SE-0303
swift-package-manager/Sources/PackagePlugin/Command.swift

The plugin with documentation is located here: https://github.com/DimaRU/PackageBuildInfo

Sample project: https://github.com/DimaRU/PackageBuildInfoExample

Expected behavior

swift package clean used to force rebuild entire project by cleanup build artifacts each run.

a-dmitry:~/Developer/temp% ./example-clean.sh
+ git clone https://github.com/DimaRU/PackageBuildInfoExample
Cloning into 'PackageBuildInfoExample'...
remote: Enumerating objects: 34, done.
remote: Counting objects: 100% (34/34), done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 34 (delta 9), reused 26 (delta 3), pack-reused 0
Receiving objects: 100% (34/34), 6.00 KiB | 2.00 MiB/s, done.
Resolving deltas: 100% (9/9), done.
+ cd PackageBuildInfoExample
+ swift run
Fetching https://github.com/DimaRU/PackageBuildInfo from cache
Fetched https://github.com/DimaRU/PackageBuildInfo (0.66s)
Creating working copy for https://github.com/DimaRU/PackageBuildInfo
Working copy of https://github.com/DimaRU/PackageBuildInfo resolved at master
Building for debugging...
[5/5] Linking PackageBuildInfoExample
Build complete! (3.71s)
Build: 1.0.0-2-g727592a
Package build info:
Time:          07.05.2022, 23:29
Branch:        master
Tag:           1.0.0
Commit count:  6
Commits since: 2
ID:            727592a42da716b59d2b0f061dbc614dc9a75892
+ touch a
+ swift package clean
+ swift run
Building for debugging...
[5/5] Linking PackageBuildInfoExample
Build complete! (3.42s)
Build: 1.0.0-2-g727592a
Package build info:
Time:          07.05.2022, 23:29
Branch:        master
Tag:           1.0.0
Commit count:  6
Commits since: 2
ID:            727592a42da716b59d2b0f061dbc614dc9a75892
+ git add a
+ swift package clean
+ swift run
Building for debugging...
[5/5] Linking PackageBuildInfoExample
Build complete! (3.38s)
Build: 1.0.0-2-g727592a-dirty
Package build info:
Time:          07.05.2022, 23:29
Branch:        master
Tag:           1.0.0
Commit count:  6
Commits since: 2
ID:            727592a42da716b59d2b0f061dbc614dc9a75892
Dirty build (have uncommitted changes)
+ git commit -m 'Add a'
[master 331aaee] Add a
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
+ swift package clean
+ swift run
Building for debugging...
[5/5] Linking PackageBuildInfoExample
Build complete! (3.40s)
Build: 1.0.0-3-g331aaee
Package build info:
Time:          08.05.2022, 09:22
Branch:        master
Tag:           1.0.0
Commit count:  7
Commits since: 3
ID:            331aaeeb8162446b25bb411a67eabb23d1723f9f

Actual behavior

a-dmitry:~/Developer/temp% ./example.sh
+ git clone https://github.com/DimaRU/PackageBuildInfoExample
Cloning into 'PackageBuildInfoExample'...
remote: Enumerating objects: 34, done.
remote: Counting objects: 100% (34/34), done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 34 (delta 9), reused 26 (delta 3), pack-reused 0
Receiving objects: 100% (34/34), 6.00 KiB | 3.00 MiB/s, done.
Resolving deltas: 100% (9/9), done.
+ cd PackageBuildInfoExample
+ swift run
Fetching https://github.com/DimaRU/PackageBuildInfo from cache
Fetched https://github.com/DimaRU/PackageBuildInfo (1.13s)
Creating working copy for https://github.com/DimaRU/PackageBuildInfo
Working copy of https://github.com/DimaRU/PackageBuildInfo resolved at master
Building for debugging...
[5/5] Linking PackageBuildInfoExample
Build complete! (3.76s)
Build: 1.0.0-2-g727592a
Package build info:
Time:          07.05.2022, 23:29
Branch:        master
Tag:           1.0.0
Commit count:  6
Commits since: 2
ID:            727592a42da716b59d2b0f061dbc614dc9a75892
+ touch a
+ swift run
Building for debugging...
[2/2] Compiling PackageBuildInfoExample packageBuildInfo.swift
Build complete! (0.71s)
Build: 1.0.0-2-g727592a
Package build info:
Time:          07.05.2022, 23:29
Branch:        master
Tag:           1.0.0
Commit count:  6
Commits since: 2
ID:            727592a42da716b59d2b0f061dbc614dc9a75892
+ git add a
+ swift run
Building for debugging...
Build complete! (0.06s)
Build: 1.0.0-2-g727592a
Package build info:
Time:          07.05.2022, 23:29
Branch:        master
Tag:           1.0.0
Commit count:  6
Commits since: 2
ID:            727592a42da716b59d2b0f061dbc614dc9a75892
+ git commit -m 'Add a'
[master b226b81] Add a
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
+ swift run
Building for debugging...
Build complete! (0.06s)
Build: 1.0.0-2-g727592a
Package build info:
Time:          07.05.2022, 23:29
Branch:        master
Tag:           1.0.0
Commit count:  6
Commits since: 2
ID:            727592a42da716b59d2b0f061dbc614dc9a75892

Steps to reproduce

  1. Install PackageBuildInfo executable: mint install DimaRU/PackageBuildInfo
  2. Use shell scripts to reproduce the problem: https://gist.github.com/DimaRU/964135c00eb3ee3cd3c7abd6f09587c8

Swift Package Manager version

swift package --version
Swift Package Manager - Swift 5.6.0

Swift & OS version (output of swift --version && uname -a)

swift --version && uname -a
swift-driver version: 1.45.2 Apple Swift version 5.6 (swiftlang-5.6.0.323.62 clang-1316.0.20.8)
Target: x86_64-apple-macosx12.0
Darwin MacBookPro.local 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64 x86_64
sw_vers
ProductName:    macOS
ProductVersion: 12.3.1
BuildVersion:   21E258
tomerd commented 2 years ago

cc @abertelrud

abertelrud commented 2 years ago

With prebuild commands it is indeed supposed to run before every build. I will have to repro with the test case to see what's going on in this case. I know that project loading is skipped on incremental builds, but the prebuild commands should be persisted and run in any case. So I think the understanding is correct, and this looks like a bug.

foxsin10 commented 2 years ago

After the first succeed running of the plugin, unless you change Package.swift or save it manually, it seems not working anymore.