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

windows: detect + handle console in os.File.Write #3376

Closed
alberts opened this issue Mar 23, 2012 · 10 comments
Closed

windows: detect + handle console in os.File.Write #3376

alberts opened this issue Mar 23, 2012 · 10 comments

Comments

@alberts
Copy link
Contributor

alberts commented Mar 23, 2012

What steps will reproduce the problem?

go run astmangle.go

What is the expected output?

text

What do you see instead?

nothing

Which compiler are you using (5g, 6g, 8g, gccgo)?

8g

Which operating system are you using?

windows

Which revision are you using?  (hg identify)

go version weekly.2012-03-22

Please provide any additional information below.

Windows 7 SP1 32-bit if that matters.

Attachments:

  1. astmangle.go (506 bytes)
@alexbrainman
Copy link
Member

Comment 1:

Please, try
go run astmangle.go 2> output.txt
and then look inside of output.txt file. It is all there. I do not know why we can't see
it on console. The output is utf8, perhaps some chars are not interpreted properly by
windows console.

Labels changed: added os-windows.

Owner changed to @alexbrainman.

Status changed to WorkingAsIntended.

@alberts
Copy link
Contributor Author

alberts commented Mar 23, 2012

Comment 2:

This makes no sense to me. Try this:
package main
import (
    "fmt"
    "os/exec"
)
func main() {
    cmd := exec.Command("go", "list", "-json", "all")
    out, err := cmd.Output()
    if err != nil {
        panic(err)
    }
    fmt.Printf("%d\n", len(out))
    fmt.Printf("%x\n", out[:32767])
}
I get about 32k characters, and then garbage

Attachments:

  1. execbork.jpg (1146720 bytes)

@alberts
Copy link
Contributor Author

alberts commented Mar 24, 2012

Comment 3:

I filed a PowerShell bug since the problem appears there too:
https://connect.microsoft.com/PowerShell/feedback/details/733023

@alexbrainman
Copy link
Member

Comment 4:

fullung,
You are correct, write fails:
package main
import (
    "fmt"
    "log"
    "os"
    "os/exec"
    "syscall"
)
func toStdout(d []byte) {
    const max = 1<<10
    for {
        if len(d) <= max {
            os.Stdout.Write(d)
            return
        }
        os.Stdout.Write(d[:max])
        d = d[max:]
    }
}
func main() {
    cmd := exec.Command("go", "list", "-json", "all")
    out, err := cmd.Output()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%d\n", len(out))
    toStdout(out)
    _, err = fmt.Printf("%x\n", out[:32767])
    if err != nil {
        log.Fatalf("errno=%d err=%v\n", int(err.(*os.PathError).Err.(syscall.Errno)), err)
    }
}
Errno=8 is ERROR_NOT_ENOUGH_MEMORY. From http://goo.gl/nVQCR :
>>>
The WriteFile function may fail with ERROR_INVALID_USER_BUFFER or
ERROR_NOT_ENOUGH_MEMORY whenever there are too many outstanding asynchronous I/O
requests.
<<<
So, I guess, you need to use smaller buffer - see toStdout in my example. My modified
program outputs:
...
        "Standard": true,
        "Root": "C:\\mingw\\go",
        "GoFiles": [
                "unsafe.go"
        ]
}
2012/03/26 11:37:59 errno=8 err=write /dev/stdout: Not enough storage is available to
process this command.
I am not sure what else we can do. So I am leaving this issue as WorkingAsIntended.
Please, let us know, if you have better ideas.
Alex

@alberts
Copy link
Contributor Author

alberts commented Mar 26, 2012

Comment 5:

I reported a bug against PowerShell with Microsoft. Let's see what they say.

@rsc
Copy link
Contributor

rsc commented Mar 26, 2012

Comment 6:

After Go 1, I think we should look into writing to the console
as a special case.  Microsoft does not really believe in 'everything
is a file'.  We should accept that and use the console writing 
functions for writing to the console, instead of the generic
file-writing function.  Detecting the console this way
would let us write small enough buffers (or whatever is
provoking this) and would also let us use the W form of the
function, so that the console would understand we're writing
UTF-8.

Labels changed: added priority-later, removed priority-triage.

Owner changed to ---.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Mar 26, 2012

Comment 7:

Issue #3346 has been merged into this issue.

@alexbrainman
Copy link
Member

Comment 8:

fullung,
I had similar problem to yours. I have created new issue
https://golang.org/issue/3767, since Russ changed subject here.
Alex

@alexbrainman
Copy link
Member

Comment 9:

http://golang.org/cl/6488044/

Status changed to Started.

@alexbrainman
Copy link
Member

Comment 10:

This issue was closed by revision 18601f8.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
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

4 participants