Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make config.nims behave like nim.cfg in terms of where these scripts are searched / run #8682

Merged
merged 7 commits into from
Aug 30, 2018

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Aug 18, 2018

/cc @Araq

  • run $systemConfigPath/config.nims if it exists
  • run ~/.config/nim/config.nims if it exists
  • run parendDir/config.nims if exists for each parendDir
  • run project config.nims if exists
  • run inputfile.nims if exists

these are controlled by `optSkipConfigFile

@timotheecour
Copy link
Member Author

timotheecour commented Aug 20, 2018

friendly ping

@alaviss
Copy link
Collaborator

alaviss commented Aug 21, 2018

Can you make it run system config.nims as well?

@timotheecour timotheecour force-pushed the pr_user_config_nims_works branch from e1da01a to c3423fd Compare August 23, 2018 23:30
@timotheecour
Copy link
Member Author

@alaviss

Can you make it run system config.nims as well?

done

PTAL

Copy link
Member

@Araq Araq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at my remarks.

compiler/nim.nim Outdated
if fileExists(scriptFile):
runNimScript(cache, scriptFile, freshDefines=false, conf)

proc runNimScriptIfExists(scriptFile:string)=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space after the colon: Just like in written English.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

compiler/nim.nim Outdated
# merge this complex logic with `loadConfigs`
# check whether these should be controlled via
# optSkipConfigFile, optSkipUserConfigFile
let config_nims = "config.nims"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a const and the Nim compiler doesn't use under_scores.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

compiler/nim.nim Outdated
elif fileExists(conf.projectPath / "config.nims"):
# directory wide NimScript file
runNimScript(cache, conf.projectPath / "config.nims", freshDefines=false, conf)
if fileExists(scriptFile) and scriptFile == conf.projectFull: return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this have a fileExists check? If the same as conf.projectFull do return. And this should probably use cmpPaths...

Copy link
Member Author

@timotheecour timotheecour Aug 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this have a fileExists check? If the same as conf.projectFull do return

it's needed; with your suggestion, nim c foo.nims would exit without error when foo.nims doesn't exist; with this PR (and in fact preexisting to this PR), it would give an error

And this should probably use cmpPaths

done

@timotheecour timotheecour force-pushed the pr_user_config_nims_works branch from 9f1283c to 7b8e420 Compare August 25, 2018 21:41
@timotheecour
Copy link
Member Author

PTAL

@timotheecour
Copy link
Member Author

@Araq friendly ping

@Araq
Copy link
Member

Araq commented Aug 27, 2018

Sorry, one more thing to do: Patch nimsuggest so that it uses the same configuration file processing.

@timotheecour timotheecour force-pushed the pr_user_config_nims_works branch from 7b8e420 to 3b8caa8 Compare August 28, 2018 02:45
@timotheecour
Copy link
Member Author

timotheecour commented Aug 28, 2018

/cc @Araq PTAL

Sorry, one more thing to do: Patch nimsuggest so that it uses the same configuration file processing.

done: did a refactoring using OOP, which removed a lot of duplicate (and out of sync!) code.
using OOP in this case is the cleanest solution IMO (avoiding as much as possible if/then/else spagetti code); it also unveiled a few things that were already out of sync in nimsuggest (eg handling of conf.projectName == "-" etc), which I fixed.

after this PR, it will also unblock #8233 which was blocking on your comment here

Good catch. But I cannot merge a PR that makes things worse, then nimsuggest disagrees with nim's way of reading the configuration.

In future PR, if needed, nimfix can also reuse cmdlinehelper to avoid being out of sync (it's been out of sync for some time; I'd rather not do the change in this PR as it's already out of sync with compiler/nim.nim)

compiler/nim.nim Outdated
proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
condsyms.initDefines(conf.symbols)
let self = ProgNim(name: "nim_compiler")
Copy link
Member Author

@timotheecour timotheecour Aug 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: this PR adds
defineSymbol conf.symbols, "nim_compiler"
for symmetry with what's done in numsuggest and nimfix which add "numsuggest" and "nimfix" respectively


proc processCmdLineAndProjectPath*(self: ProgBase, conf: ConfigRef) =
self.processCmdLine(passCmd1, "", conf)
if conf.projectName == "-":
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this branch was missing prior to this PR for nimsuggest

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And yet ... nimsuggest does not support to read the main file from stdin!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

except OSError:
conf.projectFull = conf.projectName
let p = splitFile(conf.projectFull)
let dir = if p.dir.len > 0: p.dir else: getCurrentDir()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was missing prior to this PR for nimsuggest

# 'nim foo.nims' means to just run the NimScript file and do nothing more:
if fileExists(scriptFile) and scriptFile.cmpPaths(conf.projectFull) == 0:
return false
else:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit more complex that I'd hope, but that's to keep semantics from before this PR

Copy link
Member

@Araq Araq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also hard to review because of the large diffs, who knows what features/changes were made. Maybe split it up in two PRs, one that only adds these new place where to hide config files, one that refactors the code duplication.

if fileExists(scriptFile):
runNimScript(cache, scriptFile, freshDefines=false, conf)

# TODO:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't write what's left TODO we have enough of this in the compiler, do it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

condsyms.initDefines(conf.symbols)
defineSymbol conf.symbols, self.name

proc processCmdLineAndProjectPath*(self: ProgBase, conf: ConfigRef) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this OOP approach, give processCmdLineAndProjectPath two callbacks or - probably better- make it a template.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done with 2 callbacks

compiler/nim.nim Outdated
@@ -37,7 +37,9 @@ proc prependCurDir(f: string): string =
else:
result = f

proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
type ProgNim=ref object of ProgBase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty subclass means you don't need any inheritance/OOP here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, changed it to not use OOP

@@ -487,7 +487,9 @@ proc mainThread(graph: ModuleGraph) =
var
inputThread: Thread[ThreadParams]

proc mainCommand(graph: ModuleGraph) =
type ProgNimsuggest=ref object of ProgBase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty subclass means you don't need any inheritance/OOP here.

BTW Spaces around the '='. Just like you saw it everywhere else in Nim's whole codebase.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@timotheecour timotheecour force-pushed the pr_user_config_nims_works branch 2 times, most recently from 8b8f445 to be30af0 Compare August 29, 2018 00:05
@timotheecour
Copy link
Member Author

timotheecour commented Aug 29, 2018

/cc @Araq
PTAL, all comments addressed

This is also hard to review because of the large diffs, who knows what features/changes were made. Maybe split it up in two PRs, one that only adds these new place where to hide config files, one that refactors the code duplication.

I kept different commits at strategic points and for that purpose, you can select the commit range in review, see image

one that only adds these new place where to hide config files

https://github.com/nim-lang/Nim/pull/8682/files/b76d7f1a916b080a69c588c5675afde7575d2445 (1st 4 commits)

one that refactors the code duplication.

https://github.com/nim-lang/Nim/pull/8682/files/b76d7f1a916b080a69c588c5675afde7575d2445..be30af0ccc960a7e6c1a77d9093e1327beef8ef3 (last 2 commits)

alternatively, this also helps: git fetch origin pull/8682/head:8682 && git checkout 8682 && tig

@timotheecour timotheecour changed the title ~/.config/nim/config.nims can now be used + honor project config.nims make config.nims behave like nim.cfg in terms of where these scripts are searched / run Aug 29, 2018
std/os

type NimProg* = ref object
name*: string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this field and pass name as a parameter to initDefinesProg instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

compiler/[options, idents, nimconf, scriptconfig, extccomp, commands, msgs, lineinfos, modulegraphs, condsyms],
std/os

type NimProg* = ref object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type
  NimProg* = ref object

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@timotheecour
Copy link
Member Author

/cc @Araq PTAL

@timotheecour timotheecour force-pushed the pr_user_config_nims_works branch from 3072a93 to facf529 Compare August 29, 2018 11:41
@timotheecour
Copy link
Member Author

timotheecour commented Aug 29, 2018

NOTE: rebased against #8805 ; when looking at this PR make sure to select appropriate commits in diff view => ok, that other PR was now merged, this is good to go

@timotheecour timotheecour force-pushed the pr_user_config_nims_works branch from facf529 to 3ad8f80 Compare August 29, 2018 19:48
@timotheecour
Copy link
Member Author

/cc @Araq PTAL

@Araq Araq merged commit ed0cb7b into nim-lang:devel Aug 30, 2018
@timotheecour timotheecour deleted the pr_user_config_nims_works branch August 30, 2018 17:42
@kaushalmodi
Copy link
Contributor

@timotheecour Just wanted to thank you heartily for this PR :D

kaushalmodi added a commit to kaushalmodi/Nim that referenced this pull request Oct 16, 2018
kaushalmodi added a commit to kaushalmodi/Nim that referenced this pull request Oct 16, 2018
Related: nim-lang#8682

Also mention the "nim help" command to list all available tasks.
@timotheecour
Copy link
Member Author

glad to hear, thanks :-)

Araq pushed a commit that referenced this pull request Oct 16, 2018
Related: #8682

Also mention the "nim help" command to list all available tasks.
narimiran pushed a commit to narimiran/Nim that referenced this pull request Oct 31, 2018
Related: nim-lang#8682

Also mention the "nim help" command to list all available tasks.
narimiran pushed a commit to narimiran/Nim that referenced this pull request Nov 1, 2018
Related: nim-lang#8682

Also mention the "nim help" command to list all available tasks.
narimiran pushed a commit that referenced this pull request Nov 1, 2018
Related: #8682

Also mention the "nim help" command to list all available tasks.
narimiran pushed a commit that referenced this pull request Nov 1, 2018
Related: #8682

Also mention the "nim help" command to list all available tasks.

(cherry picked from commit 3e9f506)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants