Skip to content

Commit 33e865c

Browse files
authored
tools: require the presence of a v.mod file, to install external urls via vpm (#19825)
1 parent f77e5b6 commit 33e865c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

cmd/tools/vpm/common.v

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ fn parse_query(query []string) ([]Module, []Module) {
5656
errors++
5757
continue
5858
}
59+
if !has_vmod(ident) {
60+
errors++
61+
continue
62+
}
5963
Module{
6064
name: name
6165
url: ident
@@ -93,6 +97,25 @@ fn parse_query(query []string) ([]Module, []Module) {
9397
return vpm_modules, extended_modules
9498
}
9599

100+
fn has_vmod(url string) bool {
101+
head_branch := os.execute_opt('git ls-remote --symref ${url} HEAD') or {
102+
vpm_error('failed to find git HEAD for `${url}`.', details: err.msg())
103+
return false
104+
}.output.all_after_last('/').all_before(' ').all_before('\t')
105+
url_ := if url.ends_with('.git') { url.replace('.git', '') } else { url }
106+
manifest_url := '${url_}/blob/${head_branch}/v.mod'
107+
vpm_log(@FILE_LINE, @FN, 'manifest_url: ${manifest_url}')
108+
has_vmod := http.head(manifest_url) or {
109+
vpm_error('failed to retrieve module data for `${url}`.')
110+
return false
111+
}.status_code == 200
112+
if !has_vmod {
113+
vpm_error('failed to find `v.mod` for `${url}`.')
114+
return false
115+
}
116+
return true
117+
}
118+
96119
fn get_mod_date_info(mut pp pool.PoolProcessor, idx int, wid int) &ModuleDateInfo {
97120
mut result := &ModuleDateInfo{
98121
name: pp.get_item[string](idx)

cmd/tools/vpm/install_test.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// vtest flaky: true
22
// vtest retry: 3
3+
module main
4+
35
import os
46
import v.vmod
57

@@ -108,3 +110,12 @@ fn test_missing_repo_name_in_url() {
108110
assert res.exit_code == 1
109111
assert res.output.contains('failed to retrieve module name for `${incomplete_url}`')
110112
}
113+
114+
fn test_missing_vmod_in_url() {
115+
assert has_vmod('https://github.com/vlang/v') // head branch == `master`.
116+
assert has_vmod('https://github.com/v-analyzer/v-analyzer') // head branch == `main`.
117+
assert !has_vmod('https://github.com/octocat/octocat.github.io') // not a V module.
118+
res := os.execute('${v} install https://github.com/octocat/octocat.github.io')
119+
assert res.exit_code == 1
120+
assert res.output.contains('failed to find `v.mod` for `https://github.com/octocat/octocat.github.io`'), res.output
121+
}

0 commit comments

Comments
 (0)