Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Debug: add "go run ." support #3096

Closed
chapati opened this issue Mar 12, 2020 · 9 comments
Closed

Debug: add "go run ." support #3096

chapati opened this issue Mar 12, 2020 · 9 comments

Comments

@chapati
Copy link

chapati commented Mar 12, 2020

After changes made in the issue #2085 'Start without debugging' always defaults to the current go file. The rationale behind the change was the following:

Debug: Start without Debugging is the command to use to simply run the current file rather than debugging.

Let's consider the following program.

main. go

package main
func main () {
second()
}

second.go
package main
func second() {
}

If you open main.go in your active editor and Ctrl+F5 you would get 'undefined: second'
If you open second.go in your active editor and Ctrl+F5 you would get 'runtime.main_main·f: function main is undeclared in the main package'

While I agree that cmd should not start debugging I do not see any reasons why I'm unable to 'just run' the program that has several go files. Currently I'm forced to debug, i.e. switch to the debug view, stop at each breakpoint, switch back to the editor when debug is finished, while in 90% of cases I just want to run, check output and continue editing.

The current behaviour is pretty useless. It works only for programs that have only one file with the main in it. This is pretty rare. It would be much more useful to run the whole package ("go run packagename" or "go run .") and not the current file or at least allow to override the current behaviour from launch.json. Currently this is forced in

https://github.com/microsoft/vscode-go/blob/master/src/goDebugConfiguration.ts

debugConfiguration['currentFile'] = activeEditor && activeEditor.document.languageId === 'go' && activeEditor.document.fileName;

and in https://github.com/microsoft/vscode-go/blob/master/src/debugAdapter/goDebug.ts

and there is no way to run the whole package. Let's add an option to allow running the whole package / execute "go run ." or "go run packagename". Take a look at golang/go#22726 for more details about "go run ."

@chapati chapati changed the title Debug: start without debugging defaults to current file Debug: add support of "go run ." Mar 12, 2020
@chapati chapati changed the title Debug: add support of "go run ." Debug: add "go run ." support Mar 12, 2020
@hyangah
Copy link
Contributor

hyangah commented Mar 17, 2020

I think this is a valid feature request. I arrived late so I am missing the original motivation. Currently go run takes list of .go files, package import path, ., or relative path. So accepting them seem more reasonable. What do you think? @ramya-rao-a

@chapati
Copy link
Author

chapati commented Mar 18, 2020

I think this is a valid feature request. I arrived late so I am missing the original motivation. Currently go run takes list of .go files, package import path, ., or relative path. So accepting them seem more reasonable. What do you think? @ramya-rao-a

Originally Ctrl+F5 started debugging as well. In #2085 they made it to run the file currently opened in editor. I suggest to extend the change made in #2085 and support not only simple the-whole-program-is-one-file case but make Ctrl+F5 more general. Really looking forward to having this.

@ramya-rao-a
Copy link
Contributor

How does go run . behave when there are multiple files in the current folder with main() function?

In #2085, my assumption was that the go run command only worked with files. I would be in favor of go run . and use the directory of the current file as cwd if it works as expected in the above case.

@hyangah
Copy link
Contributor

hyangah commented Mar 24, 2020

@ramya-rao-a go run . fails if there are multiple files with main() function. That is consistent how the go command and gopls would behave (i.e. failure).

go run something.go [something2.go ...] would be still useful to debug a script-like go program - is it possible to configure the launch behavior or let Ctrl+F5 to ask users to choose the behavior?

@ramya-rao-a
Copy link
Contributor

That is consistent how the go command and gopls would behave (i.e. failure).

That is good to hear.

is it possible to configure the launch behavior or let Ctrl+F5 to ask users to choose the behavior?

Without introducing any new settings or configurations, this would involve first identifying that there are such files which we generally avoid doing i.e parsing all files in the current folder which can get expensive

I have pushed a change to respect the folder path passed to the program attribute. In such cases, we will set the current working directory to the passed in value and use go run .

When one doesnt have a launch.json file at all, then the default value has always been the folder the current file belongs to.

Please try out the latest beta version of this extension which has this change and let me know how that goes

@ramya-rao-a
Copy link
Contributor

cc @quoctruong

@simpleapples
Copy link
Contributor

i think the logic about passing '.' to go run should be reconsidered. pls check this issue #3147 .

hyangah added a commit to hyangah/vscode-go-old that referenced this issue Apr 13, 2020
When "Start Debugging" or "Run without Debugging" runs without
an explicit launch configuration (launch.json), we need to use
the directory of the file as the program. That is consistent
with the default GoDebugConfigurationProvider provides as the
default DebugConfigurations. (${fileDirname}).

Update microsoft#1229 and microsoft#3096
hyangah added a commit to hyangah/vscode-go-old that referenced this issue Apr 13, 2020
When "Start Debugging" or "Run without Debugging" runs without
an explicit launch configuration (launch.json), we need to use
the directory of the file as the program. That is consistent
with the default GoDebugConfigurationProvider provides as the
default DebugConfigurations. (${fileDirname}).

Update microsoft#1229 and microsoft#3096
ramya-rao-a pushed a commit that referenced this issue Apr 14, 2020
When "Start Debugging" or "Run without Debugging" runs without
an explicit launch configuration (launch.json), we need to use
the directory of the file as the program. That is consistent
with the default GoDebugConfigurationProvider provides as the
default DebugConfigurations. (${fileDirname}).

Update #1229 and #3096
@ramya-rao-a
Copy link
Contributor

The fix associated with this issue is now available in the latest version of the extension

We will use go run . by default unless the debug configuration has the program attribute pointing a file

@chapati
Copy link
Author

chapati commented Apr 22, 2020

Thank you @ramya-rao-a @hyangah

@vscodebot vscodebot bot locked and limited conversation to collaborators May 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants