99 "github.com/please-build/gcfg"
1010
1111 "github.com/thought-machine/please/src/core"
12+ "github.com/thought-machine/please/src/plz"
1213)
1314
1415// Config prints configuration settings in human-readable format.
@@ -29,7 +30,15 @@ func Config(config *core.Configuration, options []string) {
2930
3031 values , err := gcfg .Get (config , section , subsection , name )
3132 if err != nil {
32- log .Fatalf ("Failed to get %s: %s" , option , err )
33+ if section == "plugin" {
34+ if defaultValues , ok := pluginConfigFieldDefaultValues (config , subsection , name ); ok {
35+ values = defaultValues
36+ err = nil
37+ }
38+ }
39+ if err != nil {
40+ log .Fatalf ("Failed to get %s: %s" , option , err )
41+ }
3342 }
3443
3544 for _ , value := range values {
@@ -39,6 +48,53 @@ func Config(config *core.Configuration, options []string) {
3948 }
4049}
4150
51+ // pluginConfigFieldDefaultValues returns the default values of a plugin config field and a boolean indicating whether
52+ // the plugin field actually exists.
53+ func pluginConfigFieldDefaultValues (config * core.Configuration , pluginName string , name string ) ([]string , bool ) {
54+ plugin , ok := config .Plugin [pluginName ]
55+ if ! ok {
56+ return nil , false
57+ }
58+
59+ state := core .NewBuildState (config )
60+ plz .Run ([]core.BuildLabel {plugin .Target }, nil , state , state .Config , state .TargetArch )
61+ subrepo := state .Graph .SubrepoOrDie (pluginName )
62+ subrepo .State .Initialise (subrepo )
63+
64+ for key , field := range subrepo .State .RepoConfig .PluginConfig {
65+ configKey := field .ConfigKey
66+ if configKey == "" {
67+ configKey = strings .ReplaceAll (key , "_" , "" )
68+ }
69+ if strings .EqualFold (configKey , name ) {
70+ values := normaliseBuildLabels (field .DefaultValue , subrepo .Name )
71+ return values , true
72+ }
73+ }
74+
75+ return nil , false
76+ }
77+
78+ // normaliseBuildLabels returns a copy of values with each build label made absolute.
79+ // For example, //tools/bar in subrepo foo is replaced by ///foo//tools/bar.
80+ func normaliseBuildLabels (values []string , subrepo string ) []string {
81+ valuesCopy := make ([]string , len (values ))
82+ for i , value := range values {
83+ if core .LooksLikeABuildLabel (value ) {
84+ target , annotation := core .SplitLabelAnnotation (value )
85+ if label , err := core .TryParseBuildLabel (target , "" , subrepo ); err == nil {
86+ annotatedLabel := core.AnnotatedOutputLabel {
87+ BuildLabel : label ,
88+ Annotation : annotation ,
89+ }
90+ value = annotatedLabel .String ()
91+ }
92+ }
93+ valuesCopy [i ] = value
94+ }
95+ return valuesCopy
96+ }
97+
4298// ConfigJSON prints the configuration settings as JSON.
4399func ConfigJSON (config * core.Configuration ) {
44100 data , err := gcfg .RawJSON (config )
0 commit comments