Unable to build a simple application sending a HTTP request - macos

I want to perform a HTTP request using the simplest way. I decided to use conduits. Here is my main file:
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Conduit -- the main module
-- The streaming interface uses conduits
import Data.Conduit
import Data.Conduit.Binary (sinkFile)
import qualified Data.ByteString.Lazy as L
import Control.Monad.IO.Class (liftIO)
main :: IO ()
main = do
simpleHttp "http://www.example.com/foo.txt" >>= L.writeFile "foo.txt"
And .cabal:
executable AAA
main-is: Main.hs
hs-source-dirs: src
build-depends: base ==4.6.*, text ==0.11.*, http-conduit, transformers, bytestring
I can't build it, the error is:
$ cabal build
Building AAA-0.1.0.0...
Preprocessing executable 'AAA' for
AAA-0.1.0.0...
src/Main.hs:6:8:
Could not find module `Data.Conduit.Binary'
Perhaps you meant
Data.Conduit.List (needs flag -package conduit-1.1.4)
Data.Conduit.Lift (needs flag -package conduit-1.1.4)
Use -v to see a list of the files searched for.
I've already installed all the libraries shown in the .cabal file by saying cabal install xxx.
What's up with this?
Update:
Couldn't match type `L.ByteString'
with `bytestring-0.10.0.2:Data.ByteString.Lazy.Internal.ByteString'
Expected type: bytestring-0.10.0.2:Data.ByteString.Lazy.Internal.ByteString
-> IO ()
Actual type: L.ByteString -> IO ()
In the return type of a call of `L.writeFile'
In the second argument of `(>>=)', namely `L.writeFile "foo.txt"'
In a stmt of a 'do' block:
simpleHttp "http://www.example.com/foo.txt"
>>= L.writeFile "foo.txt"

So the problem is that your program imports Data.Conduit.Binary which isn't installed. It lives in the conduit-extra package, so you have to add it to your dependencies and install it if you want to use it.
Your main function doesn't actually use it though, so you can just remove the import and it should fix the current error. You will however get a new error when attempting to build since you also import Data.Conduit which isn't listed in your cabal file either. To fix this error remove the import or add conduit to your build-depends.

Seems like now you have two (at least) versions of bytestring installed, and different packages don't agree on which one has to be used.
I suggest reinstalling http-conduit.

Related

Unable to debug go in vscode

I'm not able to debug a go file in vscode. I want to debug mrcorordinator.go in package main but the main package also contain other files.Here is the main package structure.
main
diskvd.go
lockc.go
lockd.go
mrcoordinator.go
mrworker.go
pbc.go
pbd.go
while debugging mrcoordinator.go, I'm getting Error like this
Build Error: go build -o /home/sanketbondre/myspace/6.824/src/main/__debug_bin -gcflags all=-N -l .
diskvd.go:19:8: no required module provides package 6.824/diskv; to add it:
go get 6.824/diskv
lockc.go:7:8: no required module provides package 6.824/lockservice; to add it:
go get 6.824/lockservice
pbc.go:21:8: no required module provides package 6.824/pbservice; to add it:
go get 6.824/pbservice
viewd.go:8:8: no required module provides package 6.824/viewservice; to add it:
go get 6.824/viewservice (exit status 1)
Does anyone know how debug specific go file in vscode(here I want to debug mrcoordinator.go only).

Golang: generating core failed: unable to load github.com

I installed golang and started with this tutorial: https://www.howtographql.com/graphql-go/1-getting-started/
When I run:
go run github.com/99designs/gqlgen generate
I get:
reloading module info
generating core failed: unable to load github.com/my-user/hackernews/graph/model - make sure you're using an import path to a package that exists
exit status 1
What is wrong with my setup?
This is my gopath
/Users/my-pc-user/go
TLDR
$ cd your_project_path/
$ print '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > ./tools.go
$ echo 'package model' | gofmt > ./graph/model/doc.go
$ go get .
Explanation
According to a quick start guide, you should create a package with generated code that actually is already imported by your server:
package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"your_module_name/graph"
"your_module_name/graph/generated"
)
Because of the fact that your_module_name/graph/generated has no *.go files you cannot start a server and if you try you will get an error like:
graph/schema.resolvers.go:10:2: no required module provides package your_module_name/graph/generated; to add it:
To generate that package you will need to execute go run github.com/99designs/gqlgen generate, but there is another problem: gqlgen generates code that uses another package that is still doesn't exist yet, i.e. your_module_name/graph/model.
Additional step adding build constraints is required to not drop indirect dependency while generation process. That's why there is the first step with underscore import.
And if you put any *.go file to that directory with package directive — everything should works now:
$ cd your_project_path/
$ print '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > ./tools.go
$ echo 'package model' | gofmt > ./graph/model/doc.go
$ go get .

VSCode import problem with windows API call

When importing golang.org/x/sys/windows in VSCode, I can only choose SIGDescribe, nothing else.
Hovering over the import, following errors appear.
error while importing golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows
could not import golang.org/x/sys/windows (no required module provides package "golang.org/x/sys/windows")compilerBrokenImport
The manual command go get golang.org/x/sys/windows gives the following error message
Command 'gopls.go_get_package' failed: Error: err: exit status 1: stderr: package golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows .
I already re-installed Golang and updated GoTools in VSCode, no changes.
Goal: The following code below should work.
package main
import "golang.org/x/sys/windows"
func main() {
user32DLL := windows.NewLazyDLL("user32.dll")
}
OS: Ubuntu 21.04
GO Version: 1.16.6
Editor: VSCode 1.58.1
Make a folder somewhere something. Then make a file something/main.go:
package main
import "golang.org/x/sys/windows"
func main() {
println(windows.EVENTLOG_ERROR_TYPE == 1)
}
Then build:
go mod init something
go mod tidy
go build

Resolving dependency issue while installing keter vis stack

I am trying to install the keter executable to the implicit "global-project" of stack. I use stack install keter from the directory to do this. When I do so, I get the error.
In the dependencies for keter-1.4.3.2:
http-reverse-proxy-0.5.0.1 from stack configuration does not match >=0.4.2 && <0.5 (latest matching version is 0.4.5)
unix-compat-0.5.0.1 from stack configuration does not match >=0.3 && <0.5 (latest matching version is 0.4.3.1)
needed since keter is a build target.
Some different approaches to resolving this:
* Set 'allow-newer: true' in <$HOME>/.stack/config.yaml to ignore all version constraints and build anyway.
* Consider trying 'stack solver', which uses the cabal-install solver to attempt to find some working build configuration. This can be convenient when dealing with many complicated
constraint errors, but results may be unpredictable.
* Recommended action: try adding the following to your extra-deps in <$HOME>/.stack/global-project/stack.yaml:
http-reverse-proxy-0.4.5#sha256:7f2a181ec848e8fdc0bdb4d6e775e488897957ee7ae0a2b82a623360168ccf96
unix-compat-0.4.3.1#sha256:a291f209e9c9723204c49c978ed2c53997dbc9666e53fe7bf7a3548b2c8bb22c
When I follow the recommended action, (i.e., I add the following to /.stack/global-project/stack.yaml):
extra-deps:
- http-reverse-proxy-0.4.5#sha256:7f2a181ec848e8fdc0bdb4d6e775e488897957ee7ae0a2b82a623360168ccf96
- unix-compat-0.4.3.1#sha256:a291f209e9c9723204c49c978ed2c53997dbc9666e53fe7bf7a3548b2c8bb22c
I get a different error when I do stack install keter a second time.
Completed 8 action(s).
-- While building package keter-1.4.3.2 using:
/.stack/setup-exe-cache/x86_64-linux-tinfo6/Cabal-simple_mPHDZzAJ_2.0.1.0_ghc-8.2.2 --builddir=.stack-work/dist/x86_64-linux-tinfo6/Cabal-2.0.1.0 build --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always"
Process exited with code: ExitFailure 1
Logs have been written to: /.stack/global-project/.stack-work/logs/keter-1.4.3.2.log
Configuring keter-1.4.3.2...
Preprocessing library for keter-1.4.3.2..
Building library for keter-1.4.3.2..
[ 1 of 18] Compiling Codec.Archive.TempTarball ( Codec/Archive/TempTarball.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.0.1.0/build/Codec/Archive/TempTarball.o )
[ 2 of 18] Compiling Data.Conduit.LogFile ( Data/Conduit/LogFile.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.0.1.0/build/Data/Conduit/LogFile.o )
[ 3 of 18] Compiling Data.Conduit.Process.Unix ( Data/Conduit/Process/Unix.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.0.1.0/build/Data/Conduit/Process/Unix.o )
/tmp/stack2837/keter-1.4.3.2/Data/Conduit/Process/Unix.hs:65:20: error:
• The constructor ‘ProcessHandle’ should have 3 arguments, but has been given 2
• In the pattern: ProcessHandle m _
In an equation for ‘processHandleMVar’:
processHandleMVar (ProcessHandle m _) = m
|
65 | processHandleMVar (ProcessHandle m _) = m
| ^^^^^^^^^^^^^^^^^
My guess is that keter still ends up trying to use the 0.5* versions when building, which have different signatures and so the build breaks. Any ideas on how to correctly install keter?
Here is some debug information:
`stack exec -- ghc -v`
Glasgow Haskell Compiler, Version 8.2.2, stage 2 booted by GHC version 8.0.2
Using binary package database: <$HOME>/.stack/programs/x86_64-linux/ghc-tinfo6-8.2.2/lib/ghc-8.2.2/package.conf.d/package.cache
Using binary package database: <$HOME>/.stack/snapshots/x86_64-linux-tinfo6/lts-11.5/8.2.2/pkgdb/package.cache
Using binary package database: <$HOME>/.stack/global-project/.stack-work/install/x86_64-linux-tinfo6/lts-11.5/8.2.2/pkgdb/package.cache
package flags []
loading package database <$HOME>/.stack/programs/x86_64-linux/ghc-tinfo6-8.2.2/lib/ghc-8.2.2/package.conf.d
loading package database <$HOME>/.stack/snapshots/x86_64-linux-tinfo6/lts-11.5/8.2.2/pkgdb
loading package database <$HOME>/.stack/global-project/.stack-work/install/x86_64-linux-tinfo6/lts-11.5/8.2.2/pkgdb
wired-in package ghc-prim mapped to ghc-prim-0.5.1.1
wired-in package integer-gmp mapped to integer-gmp-1.0.1.0
wired-in package base mapped to base-4.10.1.0
wired-in package rts mapped to rts
wired-in package template-haskell mapped to template-haskell-2.12.0.0
wired-in package ghc mapped to ghc-8.2.2
wired-in package dph-seq not found.
wired-in package dph-par not found.
*** Deleting temp files:
Deleting:
*** Deleting temp dirs:
Deleting:
ghc: no input files
Usage: For basic information, try the `--help' option.
stack --version
Version 1.9.1, Git revision f9d0042c141660e1d38f797e1d426be4a99b2a3c (6168 commits) x86_64 hpack-0.31.0

Stringer tool complains about wrong archive header

I am trying to use go generate/stringer (golang.org/x/tools/cmd/stringer) to generate String() methods on enums. I have problems, which I believe, are because of slightly different format of .a packages on different systems. I have this file:
package main
import (
"math/rand"
)
//go:generate stringer -type=Foo
type Foo int;
const (
FooPrime Foo = iota
FooBis
)
func main() {
//Just use rand anywhere, otherwise we get a compiler error
rand.Seed(1)
}
Now if I run go generate example.go on my machine everything is all right: foo_string.go is created. However, on a test machine I get:
stringer: checking package: example.go:4:2: could not import math/rand (reading export data: /usr/lib64/go/pkg/linux_amd64/math/rand.a: go archive is missing __.PKGDEF)
Now, after some digging in the code I think that I get this error, because on my machine rand.a has the following header:
!<arch>
__.PKGDEF 0 0 0 644 2051
`
while on test machine it has the following header:
!<arch>
__.PKGDEF/ 0 399 399 100644 2051
`
I think that the crucial difference is slash after PKGDEFF. gcimporter refuses to process .a file, if it doesn't have __.PKGDEF header.
To check this, I edited manually gcimporter/exportdata.go and changed one of the line from this:
if name != "__.PKGDEF"
to this:
if name != "__.PKGDEF" && name != "__.PKGDEF\"
After this change (and compiling and installing everything) I was able to run go generate on example.go.
My questions are: why do I get this problem and how do I get rid of it (other then manually editing external library)?
What I can see from the spec for openSUSE's packaging they are disabling reinstallation of the standard library at updates. __.PKGDEF is a Go specific informational section, and some linker OpenSUSE has used has simply produced incompatible output.
There's nothing you can do except install a healthy Go from the official source.

Resources