@@ -5,6 +5,29 @@ use std::fs;
5
5
use std:: path:: { Path , PathBuf } ;
6
6
use std:: process:: Command ;
7
7
8
+ fn codspeed_go_version ( ) -> anyhow:: Result < String > {
9
+ if std:: env:: var ( "CODSPEED_LOCAL_GO_PKG" ) . is_err ( ) && cfg ! ( not( test) ) {
10
+ return Ok ( format ! ( "v{}" , env!( "CARGO_PKG_VERSION" ) ) ) ;
11
+ }
12
+
13
+ // When running in GitHub Actions, we always want to use the latest
14
+ // codspeed-go package. For this, we have to use the current branch.
15
+ if std:: env:: var ( "GITHUB_ACTIONS" ) . is_ok ( ) {
16
+ return Ok ( std:: env:: var ( "GITHUB_HEAD_REF" ) ?) ;
17
+ }
18
+
19
+ // Locally, run `git rev-parse --abbrev-ref HEAD` to get the current branch name
20
+ let output = Command :: new ( "git" )
21
+ . args ( [ "rev-parse" , "--abbrev-ref" , "HEAD" ] )
22
+ . output ( )
23
+ . context ( "Failed to execute 'git rev-parse' command" ) ?;
24
+ if !output. status . success ( ) {
25
+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
26
+ bail ! ( "Failed to get current git branch: {}" , stderr) ;
27
+ }
28
+ Ok ( String :: from_utf8_lossy ( & output. stdout ) . trim ( ) . to_string ( ) )
29
+ }
30
+
8
31
pub fn replace_pkg < P : AsRef < Path > > ( folder : P ) -> anyhow:: Result < ( ) > {
9
32
let codspeed_root = Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) ) . parent ( ) . unwrap ( ) ;
10
33
let replace_arg = format ! (
@@ -55,8 +78,10 @@ pub fn patch_imports<P: AsRef<Path>>(
55
78
debug ! ( "Patched {patched_files} files" ) ;
56
79
57
80
// 2. Update the go module to use the codspeed package
58
- let version = format ! ( "v{}" , env!( "CARGO_PKG_VERSION" ) ) ;
59
- let pkg = format ! ( "github.com/CodSpeedHQ/codspeed-go@{version}" ) ;
81
+ let pkg = format ! (
82
+ "github.com/CodSpeedHQ/codspeed-go@{}" ,
83
+ codspeed_go_version( ) ?
84
+ ) ;
60
85
debug ! ( "Installing {pkg}" ) ;
61
86
62
87
let mut cmd: Command = Command :: new ( "go" ) ;
@@ -72,9 +97,20 @@ pub fn patch_imports<P: AsRef<Path>>(
72
97
let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
73
98
bail ! ( "Failed to install codspeed-go dependency: {}" , stderr) ;
74
99
}
75
-
76
100
debug ! ( "Successfully installed codspeed-go dependency" ) ;
77
101
102
+ // Run 'go mod tidy' to resolve transitive dependencies
103
+ let output = Command :: new ( "go" )
104
+ . args ( [ "mod" , "tidy" ] )
105
+ . current_dir ( folder)
106
+ . output ( )
107
+ . context ( "Failed to execute 'go mod tidy' command" ) ?;
108
+ if !output. status . success ( ) {
109
+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
110
+ bail ! ( "Failed to run 'go mod tidy': {}" , stderr) ;
111
+ }
112
+ debug ! ( "Ran 'go mod tidy' successfully" ) ;
113
+
78
114
// Ensure we have the latest codspeed-go package installed. Just
79
115
// use the local one which might contain uncommitted changes.
80
116
if std:: env:: var ( "CODSPEED_LOCAL_GO_PKG" ) . is_ok ( ) || cfg ! ( test) {
@@ -114,6 +150,11 @@ pub fn patch_go_source(source: &str) -> anyhow::Result<String> {
114
150
"github.com/thejerf/slogassert" ,
115
151
"\" github.com/CodSpeedHQ/codspeed-go/pkg/slogassert\" " ,
116
152
) ?;
153
+ let source = replace_import (
154
+ source,
155
+ "github.com/frankban/quicktest" ,
156
+ "\" github.com/CodSpeedHQ/codspeed-go/pkg/quicktest\" " ,
157
+ ) ?;
117
158
118
159
Ok ( source)
119
160
}
0 commit comments