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

Wrong buffer used when formating file opened in multiple views #66

Open
klmp200 opened this issue Oct 9, 2018 · 10 comments
Open

Wrong buffer used when formating file opened in multiple views #66

klmp200 opened this issue Oct 9, 2018 · 10 comments

Comments

@klmp200
Copy link

klmp200 commented Oct 9, 2018

When a file is opened multiples times in split screen it sometimes happen that when saving the wrong buffer is used for the format step with gofmt, resulting in a rollback in your modification.

@DisposaBoy
Copy link
Contributor

How can I reproduce this?

@klmp200
Copy link
Author

klmp200 commented Oct 10, 2018

I'm on Ubuntu 18.04 with Sublime Text 3176.
I split my editor in two vertical views, open a go document in the left part, switch to the right view and open the same document. When I edit this document in a section not visible in the left view and that I save (format and save) the text I edit just rollback to what was previously.

@DisposaBoy
Copy link
Contributor

Do you mean you're using the command named GoSublime: Fmt and save in the command palette?
Does this happen if you simply save the file? i.e. ctrl+s

Also which version of GoSublime are you using and what are your GoSublime (Gosublie.sublime-settings) and margo (margo.go) settings?

@klmp200
Copy link
Author

klmp200 commented Oct 10, 2018

It happens with both GoSublime: Fmt and save and ctrl+s.

For the syntax, I'm using Gosublime Go (Recommended).

Here for my margo.go settings

package margo

import (
	"time"

	"margo.sh/golang"
	"margo.sh/mg"
)

// Margo is the entry-point to margo
func Margo(ma mg.Args) {

	// add our reducers (margo plugins) to the store
	// they are run in the specified order
	// and should ideally not block for more than a couple milliseconds
	ma.Use(
		mg.NewReducer(func(mx *mg.Ctx) *mg.State {
			// By default, events (e.g. ViewSaved) are triggered in all files.
			// Replace `mg.AllLangs` with `mg.Go` to restrict events to Go(-lang) files.
			// Please note, however, that this mode is not tested
			// and saving a non-go file will not trigger linters, etc. for that go pkg
			return mx.SetConfig(mx.Config.EnabledForLangs(
				mg.Go,
			))
		}),

		// add the day and time to the status bar
		&DayTimeStatus{},

		// Add `go` command integration
		// this adds a new commands:
		// gs: these commands are all callable through 9o:
		// * go: Wrapper around the go command, adding linter support
		// * go.play: Automatically build and run go commands or run go test for packages
		//   with support for linting and unsaved files
		// * go.replay: Wrapper around go.play limited to a single instance
		//   by default this command is bound to ctrl+.,ctrl+r or cmd+.,cmd+r
		//
		// UserCmds are also added for `Go Play` and `Go RePlay`
		&golang.GoCmd{},

		// both GoFmt and GoImports will automatically disable the GoSublime version
		// you will need to install the `goimports` tool manually
		// https://godoc.org/golang.org/x/tools/cmd/goimports
		//
		// golang.GoFmt,
		// or
		golang.GoImports,

		// show func arguments/calltips in the status bar
		// gs: this replaces the `calltips` setting
		&golang.GocodeCalltips{},

		&golang.Guru{},

		// use gocode for autocompletion
		&golang.Gocode{
			// automatically install missing packages
			Autobuild: true,

			// autocompete packages that are not yet imported
			// this goes well with GoImports
			UnimportedPackages: true,

			// show the function parameters. this can take up a lot of space
			ShowFuncParams: true,
		},

		// add some default context aware-ish snippets
		golang.Snippets,

		// add our own snippets

		// check the file for syntax errors
		&golang.SyntaxCheck{},

		// add our own snippets
		MySnippets,

		// run `go install` on save
		// or use GoInstallDiscardBinaries which will additionally set $GOBIN
		// to a temp directory so binaries are not installed into your $PATH
		//
		golang.GoInstall("-i"),
		// or
		// golang.GoInstallDiscardBinaries(),

		// run `go vet` on save. go vet is ran automatically as part of `go test` in go1.10
		golang.GoVet(),

		// run `go test -race` on save
		// in go1.10, go vet is ran automatically
		golang.GoTest("-race"),

		// run `golint` on save
		// &golang.Linter{Name: "golint", Label: "Go/Lint"},

		// run gometalinter on save
		&golang.Linter{Name: "gometalinter", Args: []string{
			"--disable=gas",
			"--fast",
		}},
	)
}

// DayTimeStatus adds the current day and time to the status bar
type DayTimeStatus struct {
	mg.ReducerType
}

func (dts DayTimeStatus) ReducerMount(mx *mg.Ctx) {
	// kick off the ticker when we start
	dispatch := mx.Store.Dispatch
	go func() {
		ticker := time.NewTicker(1 * time.Second)
		for range ticker.C {
			dispatch(mg.Render)
		}
	}()
}

func (dts DayTimeStatus) Reduce(mx *mg.Ctx) *mg.State {
	// we always want to render the time
	// otherwise it will sometimes disappear from the status bar
	now := time.Now()
	format := "Mon, 15:04"
	if now.Second()%2 == 0 {
		format = "Mon, 15 04"
	}
	return mx.AddStatus(now.Format(format))
}

// MySnippets is a slice of functions returning our own snippets
var MySnippets = golang.SnippetFuncs(
	func(cx *golang.CompletionCtx) []mg.Completion {
		// if we're not in a block (i.e. function), do nothing
		if !cx.Scope.Is(golang.BlockScope) {
			return nil
		}

		return []mg.Completion{
			{
				Query: "if err",
				Title: "err != nil { return }",
				Src:   "if ${1:err} != nil {\n\treturn $0\n}",
			},
		}
	},
)

@DisposaBoy
Copy link
Contributor

Are you able to make a screen recording of the bug? I can't reproduce it and I don't have any clues how it could happen. Anything I do in one view is always replicated in the other so maybe I miss-understand the issue.

Does it happen if you press ctrl+.,ctrl+f? maybe there's a race condition of some kind when saving.

Also what version of GoSublime are you using?

@klmp200
Copy link
Author

klmp200 commented Oct 10, 2018

I tried reproducing, It happened on my company project (which I can't record for closed source reasons), tried the same on a random project where it didn't reproduce. When I got back to my company project everything worked fine and it was fixed. Usually when this bug happen I close all similar files and reopen them and it work again. Maybe it's a bad plugin combo or a bad configuration in my project settings.

I'm using Gosublime 18.10.06

I will continue to try and send you a video of this bug happening
Btw, thanks for your great plugin :)

@DisposaBoy
Copy link
Contributor

Another thing to try is disabling golang.GoImports:

  • Press ctrl+.,ctrll+x
  • find the line with golang.GoFmt or golang.GoImports and make sure they're commented out
  • save the file and margo should automatically restart (a note is added to the status bar)
  • edit and save a file in some other package (saving in the margo pkg will restart margo)
  • check that fmt no longer works (it shouldn't)
  • if it still works, it's possible that you could have multiple things fmt'ing the code, including the old version of margo.

Btw, thanks for your great plugin :)

No problem :)

@klmp200
Copy link
Author

klmp200 commented Oct 10, 2018

Commenting those line correctly disable the auto formatting.

@klmp200
Copy link
Author

klmp200 commented Oct 12, 2018

I've somehow reproduced the bug and recorded it.
It's on a really restrained view to not show the code too much, sorry for that but you can now have an example that bug.

The same file is opened in the two views and you can actually see the modifications being undone on both views at the same time.

margo-disapear

@DisposaBoy
Copy link
Contributor

  • Can you now reproduce it reliably?

  • Does it only happen in certain files, large files?, projects? etc.

  • Open the Sublime Text console and type sublime.log_commands(True) to enable command logging and see if there is anything strange in the output like undo. Type sublime.log_commands(False) to disable it.

  • Do you see anything else in the ST console?

  • What is that panel? that pops up at the bottom of the window, is it related to GoSublime?

BTW, you can send stuff to support at margo.sh if you don't want to upload to the public issue tracker.

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

No branches or pull requests

2 participants