Repository navigation
Conversation
78cad68 to
c2552bf
Compare
|
Did you have success running it? |
| } | ||
|
|
||
| // Check if --no-console was passed or if we have no args (double-clicked) | ||
| noConsole := len(args) <= 1 // No args means launched from GUI |
There was a problem hiding this comment.
noConsole := slices.Contains(args, "--no-console")
| childArgs := args[1:] // Start with original args (excluding binary name) | ||
| if noConsole { | ||
| // Check if --no-console is already in the args | ||
| hasNoConsole := false |
There was a problem hiding this comment.
hasNoConsole := slices.Contains(childArgs, "--no-console")
| procFreeConsole = kernel32.NewProc("FreeConsole") | ||
| procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow") | ||
| procSetStdHandle = kernel32.NewProc("SetStdHandle") | ||
| procCreateFileW = kernel32.NewProc("CreateFileW") |
There was a problem hiding this comment.
CreateFile and SetStdHandle are in sys/x/windows
| return nil // No command line arguments, don't allocate console | ||
| } | ||
|
|
||
| // Check if --no-console flag is present |
There was a problem hiding this comment.
if slices.Contains(os.Args[1:], "--no-console") {
return nil
}
There was a problem hiding this comment.
(Prefer if they actually used the arguments parser we have...)
|
One issue with this is if a user runs this GUI version from inside an SSH session, they won't see any output. The console window will appear on the desktop. |
There was a problem hiding this comment.
This is the approach that we didn't get to work previously. If it does work, i.e. behaves reasonably in a normal terminal etc, that's cool. I don't see why you're reinventing command line parsing multiple times instead of using the parsed command line though?
|
Thank you for the review :) I was reinventing the stuff because
I will also investigate the ssh session. Although it seems very niche to me, to ssh into a windows server. |
|
I think the SSH bit is indeed extremely niche, but probably follows the same scenario as just running the command in an existing cmd? At least the latter should work, imho. (by which I mean "show output in that terminal") |
|
This seems somewhat promising. I'll have comments on the actual code and stuff at some point, but just some behavioural notes;
|
8357fc0 to
79e5eb0
Compare
|
Hey |
|
I'm trying to test the PR right now, but for me, even when launched from Explorer, the console window just stays open. This is under Windows 10 x64, and the default shell is set to Command Prompt. |
| } | ||
|
|
||
| // User explicitly disabled console -> don't allocate console | ||
| if slices.Contains(os.Args[1:], "--no-console") { |
There was a problem hiding this comment.
I see no reason to do this instead of using our command line parser? (If there is one it's fine, but requires explanation. If it's about having a console available for when --help gets called, I suspect there is a way around that using the binding callback on the --no-console option perhaps...)
| return nil | ||
| } | ||
|
|
||
| // SSH sessions -> don't allocate console |
| // Try to attach to parent consol | ||
| if ret, _, err := procAttachConsole.Call(uintptr(ATTACH_PARENT_PROCESS)); ret != 0 { | ||
| return redirectStdHandles() | ||
| } else if err != syscall.Errno(0) { |
There was a problem hiding this comment.
This should probably be !errors.Is(err, syscall.Errno(0)) and errors.As below instead of the direct case
| if ret, _, err := procAllocConsole.Call(); ret != 0 { | ||
| consoleAllocated = true | ||
| return redirectStdHandles() | ||
| } else if err != syscall.Errno(0) { |
|
|
||
| // FreeConsole releases the console | ||
| func FreeConsole() { | ||
| if consoleAllocated { |
There was a problem hiding this comment.
I'd rather see this as a return value from InitConsole, to avoid keeping the global around...
Signed-off-by: Elias <[email protected]>
Signed-off-by: Elias <[email protected]>
Signed-off-by: Elias <[email protected]>
|
Please keep in mind, that for testing there should be no newer version online, which does not have this feature implemented. Or auto-upgrade should be disabled. |
| // 2. cmd.exe /C start (fallback): Traditional approach that spawns a command | ||
| // shell. Less secure (potential command injection) but works on all Windows | ||
| // versions including very old ones. |
There was a problem hiding this comment.
I'm pretty sure we don't run on those Windows versions...
calmh
left a comment
There was a problem hiding this comment.
Thanks, sorry for taking some time on this, I do want to get it in. Can you sort out the various style/linter complaints please, and then I think this is ready given it passes some manual testing 🙏
| var consoleAttached = false | ||
| if err := osutil.AttachConsole(); err == nil { | ||
| consoleAttached = true | ||
| } |
There was a problem hiding this comment.
| var consoleAttached = false | |
| if err := osutil.AttachConsole(); err == nil { | |
| consoleAttached = true | |
| } | |
| consoleAttached := osutil.AttachConsole() == nil |
if we don't care about the error, I guess
Signed-off-by: Elias <[email protected]>
Signed-off-by: Elias <[email protected]>
Thank you. There is no hurry 🙂 |
Signed-off-by: Elias <[email protected]>
|
Actual Windows users who want this, can we have some testing? 🙏 |
|
I want this merged, but I want people who run Windows and want this to actually test it. |
One of the near extinct windows users here, I'll test and report back, will take me some time. |
dotacow
left a comment
There was a problem hiding this comment.
Tested on Windows 10; LGTM.
built using go run build -no-upgrade.
CLI behavior is as expected.
double clicking on the primary executable functions as expected (no console).
double clicking on a shortcut:
double clicking with property modifications shows the console. (as explained in the other comment)
double clicking with the --no-console flag added to properties starts the GUI and no console, no console flash for me in any double-clicked case.
| // No command line arguments without parent (Main should have called AttachConsole already) | ||
| // means binary was probably double-clicked -> don't allocate console | ||
| if len(os.Args) <= 1 { | ||
| return false | ||
| } | ||
|
|
There was a problem hiding this comment.
This method works for detecting launching directly from the primary .exe/search bar.
But for users who use shortcuts with target flags, there is more than one argument. I stumbled upon testing while trying to separate the PR's config version from my installation, I had to pass the -home flag. example of the use case:
C:\Users\dotacow\syncthing-test\bin\syncthing.exe --home="C:\Users\dotacow\syncthing-test\test_env" "--no-console"
it is quite a niche use case (modifying target properties through a shortcut), and anyone who wants to modify target flags, should be able to add --no-console as they wish, and IMO it is better to have this be the default behavior, but I have to point it out anyways.
|
Issue (potentially blocking): when using the CLI, pressing Enter is required to get a clean prompt afterward. example: PS C:\Users\dotac\syncthing> .\bin\syncthing.exe --version
PS C:\Users\dotac\syncthing> syncthing v2.0.12-dev.40.g8a267223-pr10335 "Hafnium Hornet" (go1.26.2 windows-amd64) DESKTOP-6NU0CQ9\dotac@DESKTOP-6NU0CQ9 2025-10-27 14:57:37 UTC [modernc-sqlite, noupgrade]
# <--- (cursor blinks here, and needs manual flushing)and when you try typing the next command: PS C:\Users\dotac\syncthing> .\bin\syncthing.exe --version
PS C:\Users\dotac\syncthing> [this is me typing the next command]0.g8a267223-pr10335 "Hafnium Hornet" (go1.26.2 windows-amd64) DESKTOP-6NU0CQ9\dotac@DESKTOP-6NU0CQ9 2025-10-27 14:57:37 UTC [modernc-sqlite, noupgrade]compare this to the current binary's output: PS C:\Users\dotac\OneDrive\Desktop\syncthing-windows-amd64-v1.28.1> .\syncthing.exe --version
syncthing v2.1.0 "Hafnium Hornet" (go1.25.9 windows-amd64) builder@github.syncthing.net 2026-05-12 05:59:47 UTC
PS C:\Users\dotac\OneDrive\Desktop\syncthing-windows-amd64-v1.28.1> # <--- cursor appears here (i.e.e normal every day terminal behaviour)Hello, sorry for not catching this earlier. it wasn't really apparent through the log output, I only caught it when I recently wanted to check if I was running the latest version and accidentally ran if my concerns are unfounded please let me know, been a while since I kept up with this PR, but this could potentially break piping and other automated logging systems. I'll investigate this further. |
|
Thank you for investigating! |
|
Ok, this is much more troublesome than I initially thought. Flushing the output does not really help. There is actually a bigger culprit here. Returning the user input is not the issue. In fact, it is returned to the user even before Syncthing outputs anything. With console attachment, Syncthing starts and is able to communicate asynchronously. Meaning the console does not wait for Syncthing to terminate. It allows new user input while still receiving output from the attached Syncthing process. For example, when using the Unfortunately, on Windows there are only two options. The subsystem (
(For our case GUI would be without actual graphics) See the Microsoft documentation on console allocation policy. There are several forum posts about this and no real solution. There are some hacky workarounds that simulate the user prompt or kill the parent process, but I would not want to go down that path. However, the link above also presents a suitable solution, although it is unfortunately only available on Windows 11 24H2+: I think this PR can be closed. Dynamic console allocation would solve the GUI issue, but it would make the CUI behavior unusable. I would open another PR solely for the change in cmd/syncthing/openurl_windows.go, which is still valid for skipping I could also open another PR for the manifest approach once I have looked into it further. That would also be unrelated to dynamic console allocation. Still there is the possibility to ship two binaries: one CUI and one GUI. There are also Syncthing installers, although I have not looked into them yet. They may want to check out
(@calmh quiet ping) |
|
Firstly, thanks for your work on this, it is quite a niche feature but I am nonetheless grateful to you. but yeah your reasoning checks out to me, seems like a race condition between syncthing and the shell over the console's output buffer, but we are limited by the available Windows APIs to solve it. regarding the multiple binary approach, I actually was going to suggest that myself, though I am sure a design choice like that requires it's own careful consideration by the maintainers. |
|
We've discussed the multiple binaries approach a few times. I even had a PR open for it for quite a while. It didn't attract a lot of positive vibes.
This ^ sounds good to me. |
Dynamic Console Allocation for Windows
Purpose
Windows GUI builds either always show a short console pupup on windows systemstart implementation or never show one (CLI users can't see output).
See PR10334 , PR926 and many more
Solution
Implement dynamic console allocation:
--no-consoleflag to explicitly disable consoleTesting
Opening from explorer won't show a console.
Implementation
AllocConsole/AttachConsole) to create console on demand--no-consoleto child processes when appropriateFixes console window appearing briefly when launched from Explorer while maintaining proper CLI functionality.
Authorship