@@ -2215,3 +2215,170 @@ fn goflags_modfile_keeps_the_go_mod_replace_from_the_build() {
22152215
22162216 chmod_writable ( tmp. path ( ) ) ;
22172217}
2218+
2219+ // ── a go.sum line a downgrade left behind (#1862) ──
2220+
2221+ /// A go 1.16 consumer of the upstream at `from`, downgraded to `to` by
2222+ /// `go get`, which lowers the require but keeps `from`'s go.sum lines
2223+ /// until `go mod tidy`.
2224+ fn downgraded_consumer ( consumer : & Path , from : & str , to : & str , env : & [ ( & str , & str ) ] ) {
2225+ std:: fs:: write (
2226+ consumer. join ( "go.mod" ) ,
2227+ format ! ( "module example.com/consumer\n \n go 1.16\n \n require {UMOD} {from}\n " ) ,
2228+ )
2229+ . unwrap ( ) ;
2230+ for args in [ vec ! [ "mod" , "tidy" ] , vec ! [ "get" , & format!( "{UMOD}@{to}" ) ] ] {
2231+ let out = go ( consumer, & args, env) ;
2232+ assert ! (
2233+ out. status. success( ) ,
2234+ "go {args:?}: {}" ,
2235+ String :: from_utf8_lossy( & out. stderr)
2236+ ) ;
2237+ }
2238+ let go_mod = std:: fs:: read_to_string ( consumer. join ( "go.mod" ) ) . unwrap ( ) ;
2239+ assert ! ( go_mod. contains( & format!( "{UMOD} {to}" ) ) , "{go_mod}" ) ;
2240+ let sum = std:: fs:: read_to_string ( consumer. join ( "go.sum" ) ) . unwrap ( ) ;
2241+ assert ! (
2242+ sum. contains( & format!( "{UMOD} {from} h1:" ) ) ,
2243+ "go get must keep the stale zip line:\n {sum}"
2244+ ) ;
2245+ }
2246+
2247+ /// Publish the upstream at `version` with a greeting of `greeting`.
2248+ fn publish_upstream ( tmp : & Path , version : & str , greeting : & str ) {
2249+ publish (
2250+ tmp,
2251+ UMOD ,
2252+ version,
2253+ & [
2254+ ( "go.mod" , & format ! ( "module {UMOD}\n \n go 1.16\n " ) ) ,
2255+ (
2256+ "lib.go" ,
2257+ & format ! (
2258+ "package upstream\n \n func Greeting() string {{ return \" {greeting}\" }}\n "
2259+ ) ,
2260+ ) ,
2261+ ] ,
2262+ ) ;
2263+ }
2264+
2265+ /// #1862 case 1: downgraded below the patched version, the build links
2266+ /// the older module. The stale zip line at the patched version must not
2267+ /// pass for proof it is built: apply refuses, and a replace written
2268+ /// before the downgrade fails `apply --check` (with or without the module
2269+ /// cache to read the graph from) and is left out of VEX.
2270+ #[ test]
2271+ fn downgrade_below_the_patch_leaves_its_replace_unattested ( ) {
2272+ if !golang_e2e_matrix:: toolchain_ready ( "e2e_golang_build(downgrade-below)" ) {
2273+ return ;
2274+ }
2275+ let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
2276+ let ( consumer, modcache, proxy_url) = stage_with ( tmp. path ( ) , "1.16" ) ;
2277+ publish_upstream ( tmp. path ( ) , "v0.9.0" , "OLDER" ) ;
2278+ let env = go_env ( modcache. to_str ( ) . unwrap ( ) , & proxy_url) ;
2279+ let readonly = readonly_env ( & env) ;
2280+ let empty_cache = tmp. path ( ) . join ( "empty-modcache" ) ;
2281+ std:: fs:: create_dir_all ( & empty_cache) . unwrap ( ) ;
2282+
2283+ // A fresh apply after the downgrade refuses and writes nothing.
2284+ downgraded_consumer ( & consumer, UVER , "v0.9.0" , & env) ;
2285+ write_patch ( & consumer) ;
2286+ let ( code, so, se) = apply_json ( & consumer, & modcache) ;
2287+ assert_ne ! ( code, 0 , "apply must refuse the inert replace:\n {so}\n {se}" ) ;
2288+ assert ! (
2289+ so. contains( & format!( "the build selects {UMOD} v0.9.0" ) ) ,
2290+ "{so}\n {se}"
2291+ ) ;
2292+ assert ! ( replace_lines( & consumer. join( "go.mod" ) ) . is_empty( ) ) ;
2293+ // Applied before the downgrade: the replace is inert afterwards.
2294+ std:: fs:: write (
2295+ consumer. join ( "go.mod" ) ,
2296+ format ! ( "module example.com/consumer\n \n go 1.16\n \n require {UMOD} {UVER}\n " ) ,
2297+ )
2298+ . unwrap ( ) ;
2299+ let tidy = go ( & consumer, & [ "mod" , "tidy" ] , & env) ;
2300+ assert ! ( tidy. status. success( ) ) ;
2301+ let ( code, so, se) = apply_json ( & consumer, & modcache) ;
2302+ assert_eq ! ( code, 0 , "apply at the required version:\n {so}\n {se}" ) ;
2303+ assert ! ( go_out( & consumer, & readonly) . contains( "OUT: PATCHED" ) ) ;
2304+ let socket_replace = replace_lines ( & consumer. join ( "go.mod" ) ) ;
2305+ assert_eq ! ( socket_replace. len( ) , 1 , "{socket_replace:?}" ) ;
2306+ let get = go ( & consumer, & [ "get" , & format ! ( "{UMOD}@v0.9.0" ) ] , & env) ;
2307+ assert ! (
2308+ get. status. success( ) ,
2309+ "{}" ,
2310+ String :: from_utf8_lossy( & get. stderr)
2311+ ) ;
2312+ // The directory replace dropped the original's go.sum lines; an
2313+ // untidied go.sum from before the apply still carries them.
2314+ let mut sum = std:: fs:: read_to_string ( consumer. join ( "go.sum" ) ) . unwrap ( ) ;
2315+ if !sum. contains ( & format ! ( "{UMOD} {UVER} h1:" ) ) {
2316+ let pristine = tmp. path ( ) . join ( "pristine" ) ;
2317+ consumer_module (
2318+ & pristine,
2319+ "example.com/pristine" ,
2320+ modcache. to_str ( ) . unwrap ( ) ,
2321+ & proxy_url,
2322+ ) ;
2323+ sum. push_str ( & std:: fs:: read_to_string ( pristine. join ( "go.sum" ) ) . unwrap ( ) ) ;
2324+ std:: fs:: write ( consumer. join ( "go.sum" ) , sum) . unwrap ( ) ;
2325+ }
2326+ assert_eq ! ( replace_lines( & consumer. join( "go.mod" ) ) , socket_replace) ;
2327+ assert ! ( go_out( & consumer, & readonly) . contains( "OUT: OLDER" ) ) ;
2328+
2329+ let ( code, so, se) = check ( & consumer, & modcache) ;
2330+ assert_eq ! (
2331+ code, 1 ,
2332+ "apply --check must flag the inert replace:\n {so}\n {se}"
2333+ ) ;
2334+ assert ! (
2335+ format!( "{so}{se}" ) . contains( "is not the version the build selects (v0.9.0)" ) ,
2336+ "{so}\n {se}"
2337+ ) ;
2338+ let ( code, so, se) = check ( & consumer, & empty_cache) ;
2339+ assert_eq ! ( code, 1 , "without the cache too:\n {so}\n {se}" ) ;
2340+ assert ! (
2341+ format!( "{so}{se}" ) . contains( "nothing shows the build selects the patched version" ) ,
2342+ "{so}\n {se}"
2343+ ) ;
2344+ let r = VexRun {
2345+ offline : true ,
2346+ product : Some ( "pkg:golang/example.com/consumer" . into ( ) ) ,
2347+ ..VexRun :: default ( )
2348+ }
2349+ . env ( "GOMODCACHE" , & modcache)
2350+ . env ( "GOFLAGS" , "" ) ;
2351+ let out = run_vex ( & vex_e2e_common:: binary ( ) , & consumer, & r) ;
2352+ assert_absent ( out. doc . as_ref ( ) , UPURL ) ;
2353+ vex_e2e_common:: assert_not_attested ( & out. envelope , UPURL , "go_version_not_selected" ) ;
2354+
2355+ chmod_writable ( tmp. path ( ) ) ;
2356+ }
2357+
2358+ /// #1862 case 2: downgraded to the patched version, the build links it.
2359+ /// The stale zip line above it must not refuse the patch.
2360+ #[ test]
2361+ fn downgrade_to_the_patch_applies_and_checks_in_sync ( ) {
2362+ if !golang_e2e_matrix:: toolchain_ready ( "e2e_golang_build(downgrade-to)" ) {
2363+ return ;
2364+ }
2365+ let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
2366+ let ( consumer, modcache, proxy_url) = stage_with ( tmp. path ( ) , "1.16" ) ;
2367+ publish_upstream ( tmp. path ( ) , "v1.1.0" , "NEWER" ) ;
2368+ let env = go_env ( modcache. to_str ( ) . unwrap ( ) , & proxy_url) ;
2369+ let readonly = readonly_env ( & env) ;
2370+
2371+ downgraded_consumer ( & consumer, "v1.1.0" , UVER , & env) ;
2372+ assert ! ( go_out( & consumer, & readonly) . contains( "OUT: PRISTINE" ) ) ;
2373+ write_patch ( & consumer) ;
2374+ let ( code, so, se) = apply_json ( & consumer, & modcache) ;
2375+ assert_eq ! (
2376+ code, 0 ,
2377+ "apply must patch the selected version:\n {so}\n {se}"
2378+ ) ;
2379+ assert ! ( go_out( & consumer, & readonly) . contains( "OUT: PATCHED" ) ) ;
2380+ let ( code, so, se) = check ( & consumer, & modcache) ;
2381+ assert_eq ! ( code, 0 , "apply --check:\n {so}\n {se}" ) ;
2382+
2383+ chmod_writable ( tmp. path ( ) ) ;
2384+ }
0 commit comments